jpayne@68: """Test result object""" jpayne@68: jpayne@68: import io jpayne@68: import sys jpayne@68: import traceback jpayne@68: jpayne@68: from . import util jpayne@68: from functools import wraps jpayne@68: jpayne@68: __unittest = True jpayne@68: jpayne@68: def failfast(method): jpayne@68: @wraps(method) jpayne@68: def inner(self, *args, **kw): jpayne@68: if getattr(self, 'failfast', False): jpayne@68: self.stop() jpayne@68: return method(self, *args, **kw) jpayne@68: return inner jpayne@68: jpayne@68: STDOUT_LINE = '\nStdout:\n%s' jpayne@68: STDERR_LINE = '\nStderr:\n%s' jpayne@68: jpayne@68: jpayne@68: class TestResult(object): jpayne@68: """Holder for test result information. jpayne@68: jpayne@68: Test results are automatically managed by the TestCase and TestSuite jpayne@68: classes, and do not need to be explicitly manipulated by writers of tests. jpayne@68: jpayne@68: Each instance holds the total number of tests run, and collections of jpayne@68: failures and errors that occurred among those test runs. The collections jpayne@68: contain tuples of (testcase, exceptioninfo), where exceptioninfo is the jpayne@68: formatted traceback of the error that occurred. jpayne@68: """ jpayne@68: _previousTestClass = None jpayne@68: _testRunEntered = False jpayne@68: _moduleSetUpFailed = False jpayne@68: def __init__(self, stream=None, descriptions=None, verbosity=None): jpayne@68: self.failfast = False jpayne@68: self.failures = [] jpayne@68: self.errors = [] jpayne@68: self.testsRun = 0 jpayne@68: self.skipped = [] jpayne@68: self.expectedFailures = [] jpayne@68: self.unexpectedSuccesses = [] jpayne@68: self.shouldStop = False jpayne@68: self.buffer = False jpayne@68: self.tb_locals = False jpayne@68: self._stdout_buffer = None jpayne@68: self._stderr_buffer = None jpayne@68: self._original_stdout = sys.stdout jpayne@68: self._original_stderr = sys.stderr jpayne@68: self._mirrorOutput = False jpayne@68: jpayne@68: def printErrors(self): jpayne@68: "Called by TestRunner after test run" jpayne@68: jpayne@68: def startTest(self, test): jpayne@68: "Called when the given test is about to be run" jpayne@68: self.testsRun += 1 jpayne@68: self._mirrorOutput = False jpayne@68: self._setupStdout() jpayne@68: jpayne@68: def _setupStdout(self): jpayne@68: if self.buffer: jpayne@68: if self._stderr_buffer is None: jpayne@68: self._stderr_buffer = io.StringIO() jpayne@68: self._stdout_buffer = io.StringIO() jpayne@68: sys.stdout = self._stdout_buffer jpayne@68: sys.stderr = self._stderr_buffer jpayne@68: jpayne@68: def startTestRun(self): jpayne@68: """Called once before any tests are executed. jpayne@68: jpayne@68: See startTest for a method called before each test. jpayne@68: """ jpayne@68: jpayne@68: def stopTest(self, test): jpayne@68: """Called when the given test has been run""" jpayne@68: self._restoreStdout() jpayne@68: self._mirrorOutput = False jpayne@68: jpayne@68: def _restoreStdout(self): jpayne@68: if self.buffer: jpayne@68: if self._mirrorOutput: jpayne@68: output = sys.stdout.getvalue() jpayne@68: error = sys.stderr.getvalue() jpayne@68: if output: jpayne@68: if not output.endswith('\n'): jpayne@68: output += '\n' jpayne@68: self._original_stdout.write(STDOUT_LINE % output) jpayne@68: if error: jpayne@68: if not error.endswith('\n'): jpayne@68: error += '\n' jpayne@68: self._original_stderr.write(STDERR_LINE % error) jpayne@68: jpayne@68: sys.stdout = self._original_stdout jpayne@68: sys.stderr = self._original_stderr jpayne@68: self._stdout_buffer.seek(0) jpayne@68: self._stdout_buffer.truncate() jpayne@68: self._stderr_buffer.seek(0) jpayne@68: self._stderr_buffer.truncate() jpayne@68: jpayne@68: def stopTestRun(self): jpayne@68: """Called once after all tests are executed. jpayne@68: jpayne@68: See stopTest for a method called after each test. jpayne@68: """ jpayne@68: jpayne@68: @failfast jpayne@68: def addError(self, test, err): jpayne@68: """Called when an error has occurred. 'err' is a tuple of values as jpayne@68: returned by sys.exc_info(). jpayne@68: """ jpayne@68: self.errors.append((test, self._exc_info_to_string(err, test))) jpayne@68: self._mirrorOutput = True jpayne@68: jpayne@68: @failfast jpayne@68: def addFailure(self, test, err): jpayne@68: """Called when an error has occurred. 'err' is a tuple of values as jpayne@68: returned by sys.exc_info().""" jpayne@68: self.failures.append((test, self._exc_info_to_string(err, test))) jpayne@68: self._mirrorOutput = True jpayne@68: jpayne@68: def addSubTest(self, test, subtest, err): jpayne@68: """Called at the end of a subtest. jpayne@68: 'err' is None if the subtest ended successfully, otherwise it's a jpayne@68: tuple of values as returned by sys.exc_info(). jpayne@68: """ jpayne@68: # By default, we don't do anything with successful subtests, but jpayne@68: # more sophisticated test results might want to record them. jpayne@68: if err is not None: jpayne@68: if getattr(self, 'failfast', False): jpayne@68: self.stop() jpayne@68: if issubclass(err[0], test.failureException): jpayne@68: errors = self.failures jpayne@68: else: jpayne@68: errors = self.errors jpayne@68: errors.append((subtest, self._exc_info_to_string(err, test))) jpayne@68: self._mirrorOutput = True jpayne@68: jpayne@68: def addSuccess(self, test): jpayne@68: "Called when a test has completed successfully" jpayne@68: pass jpayne@68: jpayne@68: def addSkip(self, test, reason): jpayne@68: """Called when a test is skipped.""" jpayne@68: self.skipped.append((test, reason)) jpayne@68: jpayne@68: def addExpectedFailure(self, test, err): jpayne@68: """Called when an expected failure/error occurred.""" jpayne@68: self.expectedFailures.append( jpayne@68: (test, self._exc_info_to_string(err, test))) jpayne@68: jpayne@68: @failfast jpayne@68: def addUnexpectedSuccess(self, test): jpayne@68: """Called when a test was expected to fail, but succeed.""" jpayne@68: self.unexpectedSuccesses.append(test) jpayne@68: jpayne@68: def wasSuccessful(self): jpayne@68: """Tells whether or not this result was a success.""" jpayne@68: # The hasattr check is for test_result's OldResult test. That jpayne@68: # way this method works on objects that lack the attribute. jpayne@68: # (where would such result intances come from? old stored pickles?) jpayne@68: return ((len(self.failures) == len(self.errors) == 0) and jpayne@68: (not hasattr(self, 'unexpectedSuccesses') or jpayne@68: len(self.unexpectedSuccesses) == 0)) jpayne@68: jpayne@68: def stop(self): jpayne@68: """Indicates that the tests should be aborted.""" jpayne@68: self.shouldStop = True jpayne@68: jpayne@68: def _exc_info_to_string(self, err, test): jpayne@68: """Converts a sys.exc_info()-style tuple of values into a string.""" jpayne@68: exctype, value, tb = err jpayne@68: # Skip test runner traceback levels jpayne@68: while tb and self._is_relevant_tb_level(tb): jpayne@68: tb = tb.tb_next jpayne@68: jpayne@68: if exctype is test.failureException: jpayne@68: # Skip assert*() traceback levels jpayne@68: length = self._count_relevant_tb_levels(tb) jpayne@68: else: jpayne@68: length = None jpayne@68: tb_e = traceback.TracebackException( jpayne@68: exctype, value, tb, limit=length, capture_locals=self.tb_locals) jpayne@68: msgLines = list(tb_e.format()) jpayne@68: jpayne@68: if self.buffer: jpayne@68: output = sys.stdout.getvalue() jpayne@68: error = sys.stderr.getvalue() jpayne@68: if output: jpayne@68: if not output.endswith('\n'): jpayne@68: output += '\n' jpayne@68: msgLines.append(STDOUT_LINE % output) jpayne@68: if error: jpayne@68: if not error.endswith('\n'): jpayne@68: error += '\n' jpayne@68: msgLines.append(STDERR_LINE % error) jpayne@68: return ''.join(msgLines) jpayne@68: jpayne@68: jpayne@68: def _is_relevant_tb_level(self, tb): jpayne@68: return '__unittest' in tb.tb_frame.f_globals jpayne@68: jpayne@68: def _count_relevant_tb_levels(self, tb): jpayne@68: length = 0 jpayne@68: while tb and not self._is_relevant_tb_level(tb): jpayne@68: length += 1 jpayne@68: tb = tb.tb_next jpayne@68: return length jpayne@68: jpayne@68: def __repr__(self): jpayne@68: return ("<%s run=%i errors=%i failures=%i>" % jpayne@68: (util.strclass(self.__class__), self.testsRun, len(self.errors), jpayne@68: len(self.failures)))