diff options
-rw-r--r-- | NEWS | 6 | ||||
-rw-r--r-- | README | 14 | ||||
-rw-r--r-- | lib/testresources/__init__.py | 36 | ||||
-rw-r--r-- | lib/testresources/tests/test_test_resource.py | 20 |
4 files changed, 71 insertions, 5 deletions
@@ -5,6 +5,12 @@ testresources release notes IN DEVELOPMENT -------------- +IMPROVEMENTS +~~~~~~~~~~~~ + +* Added ``testresources.FixtureResource`` to wrap ``fixtures.Fixture`` + instances. (Robert Collins) + 0.2.4 ----- @@ -25,14 +25,18 @@ provide test optimisation where expensive common resources are needed for test cases - for example sample working trees for VCS systems, reference databases for enterprise applications, or web servers ... let imagination run wild. -Dependencies -============ +Dependencies to build/selftest +============================== * Python 2.4+ -* testtools +* testtools (http://pypi.python.org/pypi/testtools/) +* fixtures (http://pypi.python.org/pypi/fixtures) + +Dependencies to use testresources +================================= -Note that testtools is required for *running* the tests for testresources. You -can use testresources in your own app without using testtools. +* Python 2.4+ +* fixtures (http://pypi.python.org/pypi/fixtures) How testresources Works diff --git a/lib/testresources/__init__.py b/lib/testresources/__init__.py index 32d97e4..dbb2f01 100644 --- a/lib/testresources/__init__.py +++ b/lib/testresources/__init__.py @@ -616,6 +616,42 @@ class GenericResource(TestResource): return True +class FixtureResource(TestResource): + """A TestResource that decorates a ``fixtures.Fixture``. + + The fixture has its setUp and cleanUp called as expected, and + reset is called between uses. + + Due to the API of fixtures, dependency_resources are not + accessible to the wrapped fixture. However, if you are using + resource optimisation, you should wrap any dependencies in a + FixtureResource and set resources appropriately. + + See the ``fixtures`` documentation for information on managing + dependencies within the ``fixtures`` API. + + :ivar fixture: The wrapped fixture. + """ + + def __init__(self, fixture): + """Create a FixtureResource + + :param fixture: The fixture to wrap. + """ + TestResource.__init__(self) + self.fixture = fixture + + def clean(self, resource): + resource.cleanUp() + + def make(self, dependency_resources): + self.fixture.setUp() + return self.fixture + + def isDirty(self): + return True + + class ResourcedTestCase(unittest.TestCase): """A TestCase parent or utility that enables cross-test resource usage. diff --git a/lib/testresources/tests/test_test_resource.py b/lib/testresources/tests/test_test_resource.py index 97e5ab0..7cde13b 100644 --- a/lib/testresources/tests/test_test_resource.py +++ b/lib/testresources/tests/test_test_resource.py @@ -15,6 +15,7 @@ # license. # +from fixtures.tests.helpers import LoggingFixture import testtools import testresources @@ -446,3 +447,22 @@ class TestGenericResource(testtools.TestCase): resource = mgr.getResource() self.assertTrue(mgr.isDirty()) mgr.finishedWith(resource) + + +class TestFixtureResource(testtools.TestCase): + + def test_uses_setUp_cleanUp(self): + fixture = LoggingFixture() + mgr = testresources.FixtureResource(fixture) + resource = mgr.getResource() + self.assertEqual(fixture, resource) + self.assertEqual(['setUp'], fixture.calls) + mgr.finishedWith(resource) + self.assertEqual(['setUp', 'cleanUp'], fixture.calls) + + def test_always_dirty(self): + fixture = LoggingFixture() + mgr = testresources.FixtureResource(fixture) + resource = mgr.getResource() + self.assertTrue(mgr.isDirty()) + mgr.finishedWith(resource) |