From d99ef9a9df093d3443996725cd9dcac5f113f176 Mon Sep 17 00:00:00 2001 From: Michael Foord Date: Tue, 23 Feb 2010 17:00:53 +0000 Subject: [PATCH] unittest.TestResult can now be used with the TextTestRunner. TextTestRunner compatible with old TestResult objects. --- Lib/test/test_unittest.py | 19 ++++++++++++++++++- Lib/unittest/result.py | 5 ++++- Lib/unittest/runner.py | 17 ++++++++++++----- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index d009bf9ce8..048f67b097 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -2067,8 +2067,16 @@ class Test_TestResult(TestCase): 'docstring.')) classDict = dict(unittest.TestResult.__dict__) -for m in 'addSkip', 'addExpectedFailure', 'addUnexpectedSuccess': +for m in ('addSkip', 'addExpectedFailure', 'addUnexpectedSuccess', + '__init__'): del classDict[m] + +def __init__(self, stream=None, descriptions=None, verbosity=None): + self.failures = [] + self.errors = [] + self.testsRun = 0 + self.shouldStop = False +classDict['__init__'] = __init__ OldResult = type('OldResult', (object,), classDict) class Test_OldTestResult(unittest.TestCase): @@ -2113,6 +2121,15 @@ class Test_OldTestResult(unittest.TestCase): pass self.assertOldResultWarning(Test('testFoo'), 0) + def testOldResultWithRunner(self): + class Test(unittest.TestCase): + def testFoo(self): + pass + runner = unittest.TextTestRunner(resultclass=OldResult, + stream=StringIO()) + # This will raise an exception if TextTestRunner can't handle old + # test result objects + runner.run(Test('testFoo')) ### Support code for Test_TestCase ################################################################ diff --git a/Lib/unittest/result.py b/Lib/unittest/result.py index bb2a47cf4d..22e825ac61 100644 --- a/Lib/unittest/result.py +++ b/Lib/unittest/result.py @@ -16,7 +16,7 @@ class TestResult(object): contain tuples of (testcase, exceptioninfo), where exceptioninfo is the formatted traceback of the error that occurred. """ - def __init__(self): + def __init__(self, stream=None, descriptions=None, verbosity=None): self.failures = [] self.errors = [] self.testsRun = 0 @@ -25,6 +25,9 @@ class TestResult(object): self.unexpectedSuccesses = [] self.shouldStop = False + def printErrors(self): + "Called by TestRunner after test run" + def startTest(self, test): "Called when the given test is about to be run" self.testsRun += 1 diff --git a/Lib/unittest/runner.py b/Lib/unittest/runner.py index cbec296927..2fe01f6727 100644 --- a/Lib/unittest/runner.py +++ b/Lib/unittest/runner.py @@ -148,15 +148,22 @@ class TextTestRunner(object): stopTime = time.time() timeTaken = stopTime - startTime result.printErrors() - self.stream.writeln(result.separator2) + if hasattr(result, 'separator2'): + self.stream.writeln(result.separator2) run = result.testsRun self.stream.writeln("Ran %d test%s in %.3fs" % (run, run != 1 and "s" or "", timeTaken)) self.stream.writeln() - results = map(len, (result.expectedFailures, - result.unexpectedSuccesses, - result.skipped)) - expectedFails, unexpectedSuccesses, skipped = results + + expectedFails = unexpectedSuccesses = skipped = 0 + try: + results = map(len, (result.expectedFailures, + result.unexpectedSuccesses, + result.skipped)) + expectedFails, unexpectedSuccesses, skipped = results + except AttributeError: + pass + infos = [] if not result.wasSuccessful(): self.stream.write("FAILED") -- 2.40.0