diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2009-05-02 22:43:34 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2009-05-02 22:43:34 (GMT) |
commit | 07ef487a96b826584f7871629c4cc4754e41242b (patch) | |
tree | 8c41c3adc7021d39841b87ffaf44afecc50794b1 /Lib/unittest.py | |
parent | 7430989cdadfb5aacef6909a3e2c033a0209699b (diff) | |
download | cpython-07ef487a96b826584f7871629c4cc4754e41242b.zip cpython-07ef487a96b826584f7871629c4cc4754e41242b.tar.gz cpython-07ef487a96b826584f7871629c4cc4754e41242b.tar.bz2 |
Diffstat (limited to 'Lib/unittest.py')
-rw-r--r-- | Lib/unittest.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/Lib/unittest.py b/Lib/unittest.py index d5c2927..b2dc320 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -186,10 +186,22 @@ class TestResult(object): "Called when the given test is about to be run" self.testsRun = self.testsRun + 1 + def startTestRun(self): + """Called once before any tests are executed. + + See startTest for a method called before each test. + """ + def stopTest(self, test): "Called when the given test has been run" pass + def stopTestRun(self): + """Called once after all tests are executed. + + See stopTest for a method called after each test. + """ + def addError(self, test, err): """Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info(). @@ -437,8 +449,13 @@ class TestCase(object): (_strclass(self.__class__), self._testMethodName) def run(self, result=None): + orig_result = result if result is None: result = self.defaultTestResult() + startTestRun = getattr(result, 'startTestRun', None) + if startTestRun is not None: + startTestRun() + self._result = result result.startTest(self) testMethod = getattr(self, self._testMethodName) @@ -478,6 +495,10 @@ class TestCase(object): result.addSuccess(self) finally: result.stopTest(self) + if orig_result is None: + stopTestRun = getattr(result, 'stopTestRun', None) + if stopTestRun is not None: + stopTestRun() def doCleanups(self): """Execute all cleanup functions. Normally called for you after @@ -1433,7 +1454,15 @@ class TextTestRunner(object): "Run the given test case or test suite." result = self._makeResult() startTime = time.time() - test(result) + startTestRun = getattr(result, 'startTestRun', None) + if startTestRun is not None: + startTestRun() + try: + test(result) + finally: + stopTestRun = getattr(result, 'stopTestRun', None) + if stopTestRun is not None: + stopTestRun() stopTime = time.time() timeTaken = stopTime - startTime result.printErrors() |