# HG changeset patch # User Gregory Szorc # Date 2014-04-20 21:41:11 # Node ID 9a3b4f795f62c5ea00393e33c0ebe5c08ea8f978 # Parent 092b16448994353f1e97a8ce6cf93581ff9831e8 run-tests: support setUp() and tearDown() in TestCase wrapper unittest.TestCase.run() calls setUp() and tearDown() during run(). We emulate that implementation. diff --git a/tests/run-tests.py b/tests/run-tests.py --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -377,6 +377,9 @@ class Test(object): if self._threadtmp and not self._options.keep_tmpdir: shutil.rmtree(self._threadtmp, True) + def setUp(self): + """Tasks to perform before run().""" + def run(self): """Run this test instance. @@ -506,6 +509,9 @@ class Test(object): return res + def tearDown(self): + """Tasks to perform after run().""" + def _run(self, testtmp, replacements, env): # This should be implemented in child classes to run tests. return self._skip('unknown test type') @@ -1352,6 +1358,15 @@ class TestRunner(object): # with it. def run(self, result): try: + t.setUp() + except (KeyboardInterrupt, SystemExit): + raise + except Exception: + result.addError(self, sys.exc_info()) + return + + success = False + try: self.runTest() except KeyboardInterrupt: raise @@ -1366,6 +1381,17 @@ class TestRunner(object): except Exception: result.addError(self, sys.exc_info()) else: + success = True + + try: + t.tearDown() + except (KeyboardInterrupt, SystemExit): + raise + except Exception: + result.addError(self, sys.exc_info()) + success = False + + if success: result.addSuccess(self) def runTest(self):