cpython/Lib/unittest/runner.py

181 lines
5.9 KiB
Python
Raw Normal View History

2009-07-19 17:18:21 -03:00
"""Running tests"""
import sys
import time
from . import result
class _WritelnDecorator(object):
"""Used to decorate file-like objects with a handy 'writeln' method"""
def __init__(self,stream):
self.stream = stream
def __getattr__(self, attr):
if attr in ('stream', '__getstate__'):
raise AttributeError(attr)
2009-07-19 17:18:21 -03:00
return getattr(self.stream,attr)
def writeln(self, arg=None):
if arg:
self.write(arg)
self.write('\n') # text-mode streams translate to \r\n if needed
2010-02-10 10:25:12 -04:00
class TextTestResult(result.TestResult):
2009-07-19 17:18:21 -03:00
"""A test result class that can print formatted text results to a stream.
Used by TextTestRunner.
"""
separator1 = '=' * 70
separator2 = '-' * 70
def __init__(self, stream, descriptions, verbosity):
2010-02-10 10:25:12 -04:00
super(TextTestResult, self).__init__()
2009-07-19 17:18:21 -03:00
self.stream = stream
self.showAll = verbosity > 1
self.dots = verbosity == 1
self.descriptions = descriptions
def getDescription(self, test):
2010-02-10 10:25:12 -04:00
doc_first_line = test.shortDescription()
if self.descriptions and doc_first_line:
return '\n'.join((str(test), doc_first_line))
2009-07-19 17:18:21 -03:00
else:
return str(test)
def startTest(self, test):
2010-02-10 10:25:12 -04:00
super(TextTestResult, self).startTest(test)
2009-07-19 17:18:21 -03:00
if self.showAll:
self.stream.write(self.getDescription(test))
self.stream.write(" ... ")
self.stream.flush()
def addSuccess(self, test):
2010-02-10 10:25:12 -04:00
super(TextTestResult, self).addSuccess(test)
2009-07-19 17:18:21 -03:00
if self.showAll:
self.stream.writeln("ok")
elif self.dots:
self.stream.write('.')
self.stream.flush()
def addError(self, test, err):
2010-02-10 10:25:12 -04:00
super(TextTestResult, self).addError(test, err)
2009-07-19 17:18:21 -03:00
if self.showAll:
self.stream.writeln("ERROR")
elif self.dots:
self.stream.write('E')
self.stream.flush()
def addFailure(self, test, err):
2010-02-10 10:25:12 -04:00
super(TextTestResult, self).addFailure(test, err)
2009-07-19 17:18:21 -03:00
if self.showAll:
self.stream.writeln("FAIL")
elif self.dots:
self.stream.write('F')
self.stream.flush()
def addSkip(self, test, reason):
2010-02-10 10:25:12 -04:00
super(TextTestResult, self).addSkip(test, reason)
2009-07-19 17:18:21 -03:00
if self.showAll:
self.stream.writeln("skipped {0!r}".format(reason))
elif self.dots:
self.stream.write("s")
self.stream.flush()
def addExpectedFailure(self, test, err):
2010-02-10 10:25:12 -04:00
super(TextTestResult, self).addExpectedFailure(test, err)
2009-07-19 17:18:21 -03:00
if self.showAll:
self.stream.writeln("expected failure")
elif self.dots:
self.stream.write("x")
self.stream.flush()
def addUnexpectedSuccess(self, test):
2010-02-10 10:25:12 -04:00
super(TextTestResult, self).addUnexpectedSuccess(test)
2009-07-19 17:18:21 -03:00
if self.showAll:
self.stream.writeln("unexpected success")
elif self.dots:
self.stream.write("u")
self.stream.flush()
def printErrors(self):
if self.dots or self.showAll:
self.stream.writeln()
self.printErrorList('ERROR', self.errors)
self.printErrorList('FAIL', self.failures)
def printErrorList(self, flavour, errors):
for test, err in errors:
self.stream.writeln(self.separator1)
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
self.stream.writeln(self.separator2)
self.stream.writeln("%s" % err)
class TextTestRunner(object):
"""A test runner class that displays results in textual form.
It prints out the names of tests as they are run, errors as they
occur, and a summary of the results at the end of the test run.
"""
2010-02-10 10:25:12 -04:00
resultclass = TextTestResult
def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1,
resultclass=None):
2009-07-19 17:18:21 -03:00
self.stream = _WritelnDecorator(stream)
self.descriptions = descriptions
self.verbosity = verbosity
2010-02-10 10:25:12 -04:00
if resultclass is not None:
self.resultclass = resultclass
2009-07-19 17:18:21 -03:00
def _makeResult(self):
2010-02-10 10:25:12 -04:00
return self.resultclass(self.stream, self.descriptions, self.verbosity)
2009-07-19 17:18:21 -03:00
def run(self, test):
"Run the given test case or test suite."
result = self._makeResult()
startTime = time.time()
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()
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
infos = []
if not result.wasSuccessful():
self.stream.write("FAILED")
failed, errored = map(len, (result.failures, result.errors))
if failed:
infos.append("failures=%d" % failed)
if errored:
infos.append("errors=%d" % errored)
else:
self.stream.write("OK")
if skipped:
infos.append("skipped=%d" % skipped)
if expectedFails:
infos.append("expected failures=%d" % expectedFails)
if unexpectedSuccesses:
infos.append("unexpected successes=%d" % unexpectedSuccesses)
if infos:
self.stream.writeln(" (%s)" % (", ".join(infos),))
else:
self.stream.write("\n")
return result