jpayne@68: """Running tests""" jpayne@68: jpayne@68: import sys jpayne@68: import time jpayne@68: import warnings jpayne@68: jpayne@68: from . import result jpayne@68: from .signals import registerResult jpayne@68: jpayne@68: __unittest = True jpayne@68: jpayne@68: jpayne@68: class _WritelnDecorator(object): jpayne@68: """Used to decorate file-like objects with a handy 'writeln' method""" jpayne@68: def __init__(self,stream): jpayne@68: self.stream = stream jpayne@68: jpayne@68: def __getattr__(self, attr): jpayne@68: if attr in ('stream', '__getstate__'): jpayne@68: raise AttributeError(attr) jpayne@68: return getattr(self.stream,attr) jpayne@68: jpayne@68: def writeln(self, arg=None): jpayne@68: if arg: jpayne@68: self.write(arg) jpayne@68: self.write('\n') # text-mode streams translate to \r\n if needed jpayne@68: jpayne@68: jpayne@68: class TextTestResult(result.TestResult): jpayne@68: """A test result class that can print formatted text results to a stream. jpayne@68: jpayne@68: Used by TextTestRunner. jpayne@68: """ jpayne@68: separator1 = '=' * 70 jpayne@68: separator2 = '-' * 70 jpayne@68: jpayne@68: def __init__(self, stream, descriptions, verbosity): jpayne@68: super(TextTestResult, self).__init__(stream, descriptions, verbosity) jpayne@68: self.stream = stream jpayne@68: self.showAll = verbosity > 1 jpayne@68: self.dots = verbosity == 1 jpayne@68: self.descriptions = descriptions jpayne@68: jpayne@68: def getDescription(self, test): jpayne@68: doc_first_line = test.shortDescription() jpayne@68: if self.descriptions and doc_first_line: jpayne@68: return '\n'.join((str(test), doc_first_line)) jpayne@68: else: jpayne@68: return str(test) jpayne@68: jpayne@68: def startTest(self, test): jpayne@68: super(TextTestResult, self).startTest(test) jpayne@68: if self.showAll: jpayne@68: self.stream.write(self.getDescription(test)) jpayne@68: self.stream.write(" ... ") jpayne@68: self.stream.flush() jpayne@68: jpayne@68: def addSuccess(self, test): jpayne@68: super(TextTestResult, self).addSuccess(test) jpayne@68: if self.showAll: jpayne@68: self.stream.writeln("ok") jpayne@68: elif self.dots: jpayne@68: self.stream.write('.') jpayne@68: self.stream.flush() jpayne@68: jpayne@68: def addError(self, test, err): jpayne@68: super(TextTestResult, self).addError(test, err) jpayne@68: if self.showAll: jpayne@68: self.stream.writeln("ERROR") jpayne@68: elif self.dots: jpayne@68: self.stream.write('E') jpayne@68: self.stream.flush() jpayne@68: jpayne@68: def addFailure(self, test, err): jpayne@68: super(TextTestResult, self).addFailure(test, err) jpayne@68: if self.showAll: jpayne@68: self.stream.writeln("FAIL") jpayne@68: elif self.dots: jpayne@68: self.stream.write('F') jpayne@68: self.stream.flush() jpayne@68: jpayne@68: def addSkip(self, test, reason): jpayne@68: super(TextTestResult, self).addSkip(test, reason) jpayne@68: if self.showAll: jpayne@68: self.stream.writeln("skipped {0!r}".format(reason)) jpayne@68: elif self.dots: jpayne@68: self.stream.write("s") jpayne@68: self.stream.flush() jpayne@68: jpayne@68: def addExpectedFailure(self, test, err): jpayne@68: super(TextTestResult, self).addExpectedFailure(test, err) jpayne@68: if self.showAll: jpayne@68: self.stream.writeln("expected failure") jpayne@68: elif self.dots: jpayne@68: self.stream.write("x") jpayne@68: self.stream.flush() jpayne@68: jpayne@68: def addUnexpectedSuccess(self, test): jpayne@68: super(TextTestResult, self).addUnexpectedSuccess(test) jpayne@68: if self.showAll: jpayne@68: self.stream.writeln("unexpected success") jpayne@68: elif self.dots: jpayne@68: self.stream.write("u") jpayne@68: self.stream.flush() jpayne@68: jpayne@68: def printErrors(self): jpayne@68: if self.dots or self.showAll: jpayne@68: self.stream.writeln() jpayne@68: self.printErrorList('ERROR', self.errors) jpayne@68: self.printErrorList('FAIL', self.failures) jpayne@68: jpayne@68: def printErrorList(self, flavour, errors): jpayne@68: for test, err in errors: jpayne@68: self.stream.writeln(self.separator1) jpayne@68: self.stream.writeln("%s: %s" % (flavour,self.getDescription(test))) jpayne@68: self.stream.writeln(self.separator2) jpayne@68: self.stream.writeln("%s" % err) jpayne@68: jpayne@68: jpayne@68: class TextTestRunner(object): jpayne@68: """A test runner class that displays results in textual form. jpayne@68: jpayne@68: It prints out the names of tests as they are run, errors as they jpayne@68: occur, and a summary of the results at the end of the test run. jpayne@68: """ jpayne@68: resultclass = TextTestResult jpayne@68: jpayne@68: def __init__(self, stream=None, descriptions=True, verbosity=1, jpayne@68: failfast=False, buffer=False, resultclass=None, warnings=None, jpayne@68: *, tb_locals=False): jpayne@68: """Construct a TextTestRunner. jpayne@68: jpayne@68: Subclasses should accept **kwargs to ensure compatibility as the jpayne@68: interface changes. jpayne@68: """ jpayne@68: if stream is None: jpayne@68: stream = sys.stderr jpayne@68: self.stream = _WritelnDecorator(stream) jpayne@68: self.descriptions = descriptions jpayne@68: self.verbosity = verbosity jpayne@68: self.failfast = failfast jpayne@68: self.buffer = buffer jpayne@68: self.tb_locals = tb_locals jpayne@68: self.warnings = warnings jpayne@68: if resultclass is not None: jpayne@68: self.resultclass = resultclass jpayne@68: jpayne@68: def _makeResult(self): jpayne@68: return self.resultclass(self.stream, self.descriptions, self.verbosity) jpayne@68: jpayne@68: def run(self, test): jpayne@68: "Run the given test case or test suite." jpayne@68: result = self._makeResult() jpayne@68: registerResult(result) jpayne@68: result.failfast = self.failfast jpayne@68: result.buffer = self.buffer jpayne@68: result.tb_locals = self.tb_locals jpayne@68: with warnings.catch_warnings(): jpayne@68: if self.warnings: jpayne@68: # if self.warnings is set, use it to filter all the warnings jpayne@68: warnings.simplefilter(self.warnings) jpayne@68: # if the filter is 'default' or 'always', special-case the jpayne@68: # warnings from the deprecated unittest methods to show them jpayne@68: # no more than once per module, because they can be fairly jpayne@68: # noisy. The -Wd and -Wa flags can be used to bypass this jpayne@68: # only when self.warnings is None. jpayne@68: if self.warnings in ['default', 'always']: jpayne@68: warnings.filterwarnings('module', jpayne@68: category=DeprecationWarning, jpayne@68: message=r'Please use assert\w+ instead.') jpayne@68: startTime = time.perf_counter() jpayne@68: startTestRun = getattr(result, 'startTestRun', None) jpayne@68: if startTestRun is not None: jpayne@68: startTestRun() jpayne@68: try: jpayne@68: test(result) jpayne@68: finally: jpayne@68: stopTestRun = getattr(result, 'stopTestRun', None) jpayne@68: if stopTestRun is not None: jpayne@68: stopTestRun() jpayne@68: stopTime = time.perf_counter() jpayne@68: timeTaken = stopTime - startTime jpayne@68: result.printErrors() jpayne@68: if hasattr(result, 'separator2'): jpayne@68: self.stream.writeln(result.separator2) jpayne@68: run = result.testsRun jpayne@68: self.stream.writeln("Ran %d test%s in %.3fs" % jpayne@68: (run, run != 1 and "s" or "", timeTaken)) jpayne@68: self.stream.writeln() jpayne@68: jpayne@68: expectedFails = unexpectedSuccesses = skipped = 0 jpayne@68: try: jpayne@68: results = map(len, (result.expectedFailures, jpayne@68: result.unexpectedSuccesses, jpayne@68: result.skipped)) jpayne@68: except AttributeError: jpayne@68: pass jpayne@68: else: jpayne@68: expectedFails, unexpectedSuccesses, skipped = results jpayne@68: jpayne@68: infos = [] jpayne@68: if not result.wasSuccessful(): jpayne@68: self.stream.write("FAILED") jpayne@68: failed, errored = len(result.failures), len(result.errors) jpayne@68: if failed: jpayne@68: infos.append("failures=%d" % failed) jpayne@68: if errored: jpayne@68: infos.append("errors=%d" % errored) jpayne@68: else: jpayne@68: self.stream.write("OK") jpayne@68: if skipped: jpayne@68: infos.append("skipped=%d" % skipped) jpayne@68: if expectedFails: jpayne@68: infos.append("expected failures=%d" % expectedFails) jpayne@68: if unexpectedSuccesses: jpayne@68: infos.append("unexpected successes=%d" % unexpectedSuccesses) jpayne@68: if infos: jpayne@68: self.stream.writeln(" (%s)" % (", ".join(infos),)) jpayne@68: else: jpayne@68: self.stream.write("\n") jpayne@68: return result