summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-02-23 17:00:53 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-02-23 17:00:53 (GMT)
commitd99ef9a9df093d3443996725cd9dcac5f113f176 (patch)
treea085a6242883b616fdbed2c61cdcb84df1bb6140 /Lib
parentcf80f04b16e25d05f60ca9a2e94cbf742b01d47a (diff)
downloadcpython-d99ef9a9df093d3443996725cd9dcac5f113f176.zip
cpython-d99ef9a9df093d3443996725cd9dcac5f113f176.tar.gz
cpython-d99ef9a9df093d3443996725cd9dcac5f113f176.tar.bz2
unittest.TestResult can now be used with the TextTestRunner. TextTestRunner compatible with old TestResult objects.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_unittest.py19
-rw-r--r--Lib/unittest/result.py5
-rw-r--r--Lib/unittest/runner.py17
3 files changed, 34 insertions, 7 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py
index d009bf9..048f67b 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 bb2a47c..22e825a 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 cbec296..2fe01f6 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")