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