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