diff options
author | Jonathan Lange <jml@mumak.net> | 2015-12-04 18:10:37 +0000 |
---|---|---|
committer | Jonathan Lange <jml@mumak.net> | 2015-12-04 18:10:37 +0000 |
commit | d5db0115c33f8e22eedb750b9163e1bd817e13df (patch) | |
tree | 4fb2f05f8ff74201fce641969aca8b27dbf9e0e0 | |
parent | 70770264fa103916dce33a5ad11410611e23eba6 (diff) | |
download | testtools-d5db0115c33f8e22eedb750b9163e1bd817e13df.tar.gz |
Allow pre setUp & post tearDown to be set
-rw-r--r-- | testtools/tests/samplecases.py | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/testtools/tests/samplecases.py b/testtools/tests/samplecases.py index 2120980..5e4d063 100644 --- a/testtools/tests/samplecases.py +++ b/testtools/tests/samplecases.py @@ -18,7 +18,8 @@ from testtools.matchers import ( def make_test_case(test_method_name, set_up=None, test_body=None, - tear_down=None, cleanups=()): + tear_down=None, cleanups=(), pre_set_up=None, + post_tear_down=None): """Make a test case with the given behaviors. All callables are unary callables that receive this test as their argument. @@ -29,29 +30,38 @@ def make_test_case(test_method_name, set_up=None, test_body=None, assigned to the test method. :param callable tear_down: Implementation of tearDown. :param cleanups: Iterable of callables that will be added as cleanups. + :param callable pre_set_up: Called before the upcall to setUp(). + :param callable post_tear_down: Called after the upcall to tearDown(). :return: A ``testtools.TestCase``. """ set_up = set_up if set_up else _do_nothing test_body = test_body if test_body else _do_nothing tear_down = tear_down if tear_down else _do_nothing + pre_set_up = pre_set_up if pre_set_up else _do_nothing + post_tear_down = post_tear_down if post_tear_down else _do_nothing return _ConstructedTest( - test_method_name, set_up, test_body, tear_down, cleanups) + test_method_name, set_up, test_body, tear_down, cleanups, + pre_set_up, post_tear_down, + ) class _ConstructedTest(TestCase): """A test case where all of the stages.""" def __init__(self, test_method_name, set_up, test_body, tear_down, - cleanups): + cleanups, pre_set_up, post_tear_down): setattr(self, test_method_name, test_body) super(_ConstructedTest, self).__init__(test_method_name) self._set_up = set_up self._test_body = test_body self._tear_down = tear_down self._test_cleanups = cleanups + self._pre_set_up = pre_set_up + self._post_tear_down = post_tear_down def setUp(self): + self._pre_set_up(self) super(_ConstructedTest, self).setUp() for cleanup in self._test_cleanups: self.addCleanup(cleanup, self) @@ -63,6 +73,7 @@ class _ConstructedTest(TestCase): def tearDown(self): self._tear_down(self) super(_ConstructedTest, self).tearDown() + self._post_tear_down(self) def _do_nothing(case): |