summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Lange <jml@mumak.net>2015-12-22 16:57:46 +0000
committerJonathan Lange <jml@mumak.net>2015-12-22 16:57:46 +0000
commit12bcb66c329b5f6890aa3e283692aed263f0687b (patch)
treeb784eecbe7bf9f66f7171d5cd49efde2944a7ba5
parent371781543625dd1560972c5f3b2f649f8d08f776 (diff)
downloadfixtures-git-use-fixture-function.tar.gz
Extract function for use_fixtureuse-fixture-function
-rw-r--r--fixtures/testcase.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/fixtures/testcase.py b/fixtures/testcase.py
index 1d6a85c..032906a 100644
--- a/fixtures/testcase.py
+++ b/fixtures/testcase.py
@@ -22,9 +22,37 @@ import unittest
from fixtures.fixture import gather_details
+def use_fixture(case, fixture):
+ """Use fixture on a test case.
+
+ :param case: An object that has ``addCleanup`` and optionally support for
+ details.
+ :param fixture: The fixture to use.
+ :return: The fixture, after setting it up and scheduling a cleanup for it.
+ """
+ use_details = (
+ gather_details is not None and
+ getattr(case, "addDetail", None) is not None)
+ try:
+ fixture.setUp()
+ except:
+ if use_details:
+ # Capture the details now, in case the fixture goes away.
+ gather_details(fixture.getDetails(), case.getDetails())
+ raise
+ else:
+ case.addCleanup(fixture.cleanUp)
+ if use_details:
+ # Capture the details from the fixture during test teardown;
+ # this will evaluate the details before tearing down the
+ # fixture.
+ case.addCleanup(gather_details, fixture, case)
+ return fixture
+
+
class TestWithFixtures(unittest.TestCase):
"""A TestCase with a helper function to use fixtures.
-
+
Normally used as a mix-in class to add useFixture.
Note that test classes such as testtools.TestCase which already have a
@@ -40,21 +68,4 @@ class TestWithFixtures(unittest.TestCase):
:return: The fixture, after setting it up and scheduling a cleanup for
it.
"""
- use_details = (
- gather_details is not None and
- getattr(self, "addDetail", None) is not None)
- try:
- fixture.setUp()
- except:
- if use_details:
- # Capture the details now, in case the fixture goes away.
- gather_details(fixture.getDetails(), self.getDetails())
- raise
- else:
- self.addCleanup(fixture.cleanUp)
- if use_details:
- # Capture the details from the fixture during test teardown;
- # this will evaluate the details before tearing down the
- # fixture.
- self.addCleanup(gather_details, fixture, self)
- return fixture
+ return use_fixture(self, fixture)