Kaydet (Commit) d99ef9a9 authored tarafından Michael Foord's avatar Michael Foord

unittest.TestResult can now be used with the TextTestRunner. TextTestRunner…

unittest.TestResult can now be used with the TextTestRunner. TextTestRunner compatible with old TestResult objects.
üst cf80f04b
......@@ -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
################################################################
......
......@@ -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
......
......@@ -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")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment