diff options
author | Robert Collins <robertc@robertcollins.net> | 2010-10-20 11:01:39 +1300 |
---|---|---|
committer | Robert Collins <robertc@robertcollins.net> | 2010-10-20 11:01:39 +1300 |
commit | eb5d95fc4dad79dca20c65afb0fe77f8dc74a31e (patch) | |
tree | c51fcadd50e1dac72c235530ede002e101828dd1 | |
parent | 98d5abe4e0449ec6af1ef03cbaec61389de831e4 (diff) | |
download | fixtures-eb5d95fc4dad79dca20c65afb0fe77f8dc74a31e.tar.gz |
Fixtures now have a ``useFixture`` method as well, making nesting of fixtures
trivial.
-rw-r--r-- | NEWS | 11 | ||||
-rw-r--r-- | README | 5 | ||||
-rw-r--r-- | lib/fixtures/fixture.py | 13 | ||||
-rw-r--r-- | lib/fixtures/tests/helpers.py | 13 | ||||
-rw-r--r-- | lib/fixtures/tests/test_fixture.py | 10 | ||||
-rwxr-xr-x | setup.py | 2 |
6 files changed, 48 insertions, 6 deletions
@@ -6,6 +6,17 @@ fixtures release notes IN DEVELOPMENT ~~~~~~~~~~~~~~ +0.3.3 +~~~~~ + +Fixtures now have a ``useFixture`` method as well, making nesting of fixtures +trivial. + +CHANGES: + +* New method ``Fixture.useFixture`` allowing fixtures to compose other + fixtures. (Robert Collins) + 0.3.2 ~~~~~ @@ -28,10 +28,15 @@ Dependencies * Python 2.4+ For use in a unit test suite using the included glue, one of: + * Python 2.7 + * unittest2 + * bzrlib.tests + * testtools <https://launchpad.net/testtools> + * Or any other test environment that supports TestCase.addCleanup. Writing your own glue code is easy, or you can simply use Fixtures directly diff --git a/lib/fixtures/fixture.py b/lib/fixtures/fixture.py index 60522bc..39ec631 100644 --- a/lib/fixtures/fixture.py +++ b/lib/fixtures/fixture.py @@ -132,6 +132,19 @@ class Fixture(object): self.cleanUp() self.setUp() + def useFixture(self, fixture): + """Use another fixture. + + The fixture will be setUp, and self.addCleanup(fixture.cleanUp) called. + + :param fixture: The fixture to use. + :return: The fixture, after setting it up and scheduling a cleanup for + it. + """ + fixture.setUp() + self.addCleanup(fixture.cleanUp) + return fixture + class FunctionFixture(Fixture): """An adapter to use function(s) as a Fixture. diff --git a/lib/fixtures/tests/helpers.py b/lib/fixtures/tests/helpers.py index 147de80..ae0d8d3 100644 --- a/lib/fixtures/tests/helpers.py +++ b/lib/fixtures/tests/helpers.py @@ -17,14 +17,17 @@ import fixtures class LoggingFixture(fixtures.Fixture): - def __init__(self): + def __init__(self, suffix='', calls=None): super(LoggingFixture, self).__init__() - self.calls = [] + if calls is None: + calls = [] + self.calls = calls + self.suffix = suffix def setUp(self): super(LoggingFixture, self).setUp() - self.calls.append('setUp') - self.addCleanup(self.calls.append, 'cleanUp') + self.calls.append('setUp' + self.suffix) + self.addCleanup(self.calls.append, 'cleanUp' + self.suffix) def reset(self): - self.calls.append('reset') + self.calls.append('reset' + self.suffix) diff --git a/lib/fixtures/tests/test_fixture.py b/lib/fixtures/tests/test_fixture.py index 178939b..474344c 100644 --- a/lib/fixtures/tests/test_fixture.py +++ b/lib/fixtures/tests/test_fixture.py @@ -100,6 +100,16 @@ class TestFixture(testtools.TestCase): self.assertEqual(('hoo',), exc.args[1][1].args) self.assertEqual(['1', '2'], calls) + def test_useFixture(self): + parent = LoggingFixture('-outer') + nested = LoggingFixture('-inner', calls=parent.calls) + parent.setUp() + parent.useFixture(nested) + parent.cleanUp() + self.assertEqual( + ['setUp-outer', 'setUp-inner', 'cleanUp-inner', 'cleanUp-outer'], + parent.calls) + class TestFunctionFixture(testtools.TestCase): @@ -6,7 +6,7 @@ import os.path description = file(os.path.join(os.path.dirname(__file__), 'README'), 'rb').read() setup(name="fixtures", - version="0.3.2", + version="0.3.3", description="Fixtures, reusable state for writing clean tests and more.", long_description=description, maintainer="Robert Collins", |