run-tests: support setUp() and tearDown() in TestCase wrapper
authorGregory Szorc <gregory.szorc@gmail.com>
Sun, 20 Apr 2014 14:41:11 -0700
changeset 21446 9a3b4f795f62
parent 21445 092b16448994
child 21447 f8c5b8a288c5
run-tests: support setUp() and tearDown() in TestCase wrapper unittest.TestCase.run() calls setUp() and tearDown() during run(). We emulate that implementation.
tests/run-tests.py
--- a/tests/run-tests.py	Sun Apr 20 14:34:03 2014 -0700
+++ b/tests/run-tests.py	Sun Apr 20 14:41:11 2014 -0700
@@ -377,6 +377,9 @@
         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 @@
 
         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 @@
             # 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 @@
                 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):