Add support for informing stop_fixture of any exception

This makes it possible for stop_fixture to decide if it wants to
cleanup or not. For some exceptions it might not make sense.

To test this, non-gabbi tests now exist in gabbi/tests/ with the
dreaded introduction of mocks.
This commit is contained in:
Chris Dent
2015-05-26 22:45:38 +01:00
parent a57d938faf
commit 5bf48292af
5 changed files with 62 additions and 0 deletions

View File

@@ -31,3 +31,9 @@ If a fixture raises ``unittest.case.SkipTest`` during
This makes it possible to skip the tests if some optional
configuration (such as a particular type of database) is not
available.
If an exception is raised while a fixture is being used, information
about the exception will be stored on the fixture so that the
``stop_fixture`` method can decide if the exception should change how
the fixture should clean up. The exception information can be found on
``exc_type``, ``exc_value`` and ``traceback`` method attributes.

View File

@@ -42,10 +42,18 @@ class GabbiFixture(object):
Otherwise exception handling will not work properly.
"""
def __init__(self):
self.exc_type = None
self.exc_value = None
self.traceback = None
def __enter__(self):
self.start_fixture()
def __exit__(self, exc_type, value, traceback):
self.exc_type = exc_type
self.exc_value = value
self.traceback = traceback
self.stop_fixture()
def start_fixture(self):

0
gabbi/tests/__init__.py Normal file
View File

View File

@@ -0,0 +1,47 @@
"""
Use mocks to confirm that fixtures operate as context managers.
"""
import mock
import testtools
from gabbi import fixture
class FakeFixture(fixture.GabbiFixture):
def __init__(self, mock):
super(FakeFixture, self).__init__()
self.mock = mock
def start_fixture(self):
self.mock.start()
def stop_fixture(self):
if self.exc_type:
self.mock.handle(self.exc_type)
self.mock.stop()
class FixtureTest(testtools.TestCase):
def setUp(self):
super(FixtureTest, self).setUp()
self.magic = mock.MagicMock(['start', 'stop', 'handle'])
def test_fixture_starts_and_stop(self):
with FakeFixture(self.magic):
pass
self.magic.start.assert_called_once_with()
self.magic.stop.assert_called_once_with()
def test_fixture_informs_on_exception(self):
"""Test that the stop fixture is passed exception info."""
try:
with FakeFixture(self.magic):
raise ValueError()
except ValueError:
pass
self.magic.start.assert_called_once_with()
self.magic.stop.assert_called_once_with()
self.magic.handle.assert_called_once_with(ValueError)

View File

@@ -1,3 +1,4 @@
mock
tox
testrepository
coverage