from .signals import registerResult
class _WritelnDecorator(object):
"""Used to decorate file-like objects with a handy 'writeln' method"""
def __init__(self,stream):
def __getattr__(self, attr):
if attr in ('stream', '__getstate__'):
raise AttributeError(attr)
return getattr(self.stream,attr)
def writeln(self, arg=None):
self.write('\n') # text-mode streams translate to \r\n if needed
class TextTestResult(result.TestResult):
"""A test result class that can print formatted text results to a stream.
def __init__(self, stream, descriptions, verbosity):
super(TextTestResult, self).__init__(stream, descriptions, verbosity)
self.showAll = verbosity > 1
self.dots = verbosity == 1
self.descriptions = descriptions
def getDescription(self, test):
doc_first_line = test.shortDescription()
if self.descriptions and doc_first_line:
return '\n'.join((str(test), doc_first_line))
def startTest(self, test):
super(TextTestResult, self).startTest(test)
self.stream.write(self.getDescription(test))
self.stream.write(" ... ")
def addSuccess(self, test):
super(TextTestResult, self).addSuccess(test)
self.stream.writeln("ok")
def addError(self, test, err):
super(TextTestResult, self).addError(test, err)
self.stream.writeln("ERROR")
def addFailure(self, test, err):
super(TextTestResult, self).addFailure(test, err)
self.stream.writeln("FAIL")
def addSkip(self, test, reason):
super(TextTestResult, self).addSkip(test, reason)
self.stream.writeln("skipped {0!r}".format(reason))
def addExpectedFailure(self, test, err):
super(TextTestResult, self).addExpectedFailure(test, err)
self.stream.writeln("expected failure")
def addUnexpectedSuccess(self, test):
super(TextTestResult, self).addUnexpectedSuccess(test)
self.stream.writeln("unexpected success")
if self.dots or self.showAll:
self.printErrorList('ERROR', self.errors)
self.printErrorList('FAIL', self.failures)
def printErrorList(self, flavour, 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.
resultclass = TextTestResult
def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1,
failfast=False, buffer=False, resultclass=None):
self.stream = _WritelnDecorator(stream)
self.descriptions = descriptions
self.verbosity = verbosity
if resultclass is not None:
self.resultclass = resultclass
return self.resultclass(self.stream, self.descriptions, self.verbosity)
"Run the given test case or test suite."
result = self._makeResult()
result.failfast = self.failfast
result.buffer = self.buffer
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
timeTaken = stopTime - startTime
if hasattr(result, 'separator2'):
self.stream.writeln(result.separator2)
self.stream.writeln("Ran %d test%s in %.3fs" %
(run, run != 1 and "s" or "", timeTaken))
expectedFails = unexpectedSuccesses = skipped = 0
results = map(len, (result.expectedFailures,
result.unexpectedSuccesses,
expectedFails, unexpectedSuccesses, skipped = results
if not result.wasSuccessful():
self.stream.write("FAILED")
failed, errored = map(len, (result.failures, result.errors))
infos.append("failures=%d" % failed)
infos.append("errors=%d" % errored)
infos.append("skipped=%d" % skipped)
infos.append("expected failures=%d" % expectedFails)
infos.append("unexpected successes=%d" % unexpectedSuccesses)
self.stream.writeln(" (%s)" % (", ".join(infos),))