2009-07-19 17:18:21 -03:00
|
|
|
"""Test result object"""
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from . import util
|
2010-03-21 22:01:34 -03:00
|
|
|
from functools import wraps
|
2009-07-19 17:18:21 -03:00
|
|
|
|
2010-03-21 21:06:30 -03:00
|
|
|
__unittest = True
|
|
|
|
|
2010-03-21 22:01:34 -03:00
|
|
|
def failfast(method):
|
|
|
|
@wraps(method)
|
|
|
|
def inner(self, *args, **kw):
|
|
|
|
if getattr(self, 'failfast', False):
|
|
|
|
self.stop()
|
|
|
|
return method(self, *args, **kw)
|
|
|
|
return inner
|
2009-07-19 17:18:21 -03:00
|
|
|
|
|
|
|
class TestResult(object):
|
|
|
|
"""Holder for test result information.
|
|
|
|
|
|
|
|
Test results are automatically managed by the TestCase and TestSuite
|
|
|
|
classes, and do not need to be explicitly manipulated by writers of tests.
|
|
|
|
|
|
|
|
Each instance holds the total number of tests run, and collections of
|
|
|
|
failures and errors that occurred among those test runs. The collections
|
|
|
|
contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
|
|
|
|
formatted traceback of the error that occurred.
|
|
|
|
"""
|
2010-03-07 18:04:55 -04:00
|
|
|
_previousTestClass = None
|
|
|
|
_moduleSetUpFailed = False
|
2010-02-23 13:00:53 -04:00
|
|
|
def __init__(self, stream=None, descriptions=None, verbosity=None):
|
2010-03-21 22:01:34 -03:00
|
|
|
self.failfast = False
|
2009-07-19 17:18:21 -03:00
|
|
|
self.failures = []
|
|
|
|
self.errors = []
|
|
|
|
self.testsRun = 0
|
|
|
|
self.skipped = []
|
|
|
|
self.expectedFailures = []
|
|
|
|
self.unexpectedSuccesses = []
|
|
|
|
self.shouldStop = False
|
|
|
|
|
2010-02-23 13:00:53 -04:00
|
|
|
def printErrors(self):
|
|
|
|
"Called by TestRunner after test run"
|
|
|
|
|
2009-07-19 17:18:21 -03:00
|
|
|
def startTest(self, test):
|
|
|
|
"Called when the given test is about to be run"
|
2010-02-10 10:25:12 -04:00
|
|
|
self.testsRun += 1
|
2009-07-19 17:18:21 -03:00
|
|
|
|
|
|
|
def startTestRun(self):
|
|
|
|
"""Called once before any tests are executed.
|
|
|
|
|
|
|
|
See startTest for a method called before each test.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def stopTest(self, test):
|
2010-02-10 10:25:12 -04:00
|
|
|
"""Called when the given test has been run"""
|
2009-07-19 17:18:21 -03:00
|
|
|
|
|
|
|
def stopTestRun(self):
|
|
|
|
"""Called once after all tests are executed.
|
|
|
|
|
|
|
|
See stopTest for a method called after each test.
|
|
|
|
"""
|
|
|
|
|
2010-03-21 22:01:34 -03:00
|
|
|
@failfast
|
2009-07-19 17:18:21 -03:00
|
|
|
def addError(self, test, err):
|
|
|
|
"""Called when an error has occurred. 'err' is a tuple of values as
|
|
|
|
returned by sys.exc_info().
|
|
|
|
"""
|
|
|
|
self.errors.append((test, self._exc_info_to_string(err, test)))
|
|
|
|
|
2010-03-21 22:01:34 -03:00
|
|
|
@failfast
|
2009-07-19 17:18:21 -03:00
|
|
|
def addFailure(self, test, err):
|
|
|
|
"""Called when an error has occurred. 'err' is a tuple of values as
|
|
|
|
returned by sys.exc_info()."""
|
|
|
|
self.failures.append((test, self._exc_info_to_string(err, test)))
|
|
|
|
|
|
|
|
def addSuccess(self, test):
|
|
|
|
"Called when a test has completed successfully"
|
|
|
|
pass
|
|
|
|
|
|
|
|
def addSkip(self, test, reason):
|
|
|
|
"""Called when a test is skipped."""
|
|
|
|
self.skipped.append((test, reason))
|
|
|
|
|
|
|
|
def addExpectedFailure(self, test, err):
|
|
|
|
"""Called when an expected failure/error occured."""
|
|
|
|
self.expectedFailures.append(
|
|
|
|
(test, self._exc_info_to_string(err, test)))
|
|
|
|
|
2010-03-21 22:01:34 -03:00
|
|
|
@failfast
|
2009-07-19 17:18:21 -03:00
|
|
|
def addUnexpectedSuccess(self, test):
|
|
|
|
"""Called when a test was expected to fail, but succeed."""
|
|
|
|
self.unexpectedSuccesses.append(test)
|
|
|
|
|
|
|
|
def wasSuccessful(self):
|
|
|
|
"Tells whether or not this result was a success"
|
|
|
|
return len(self.failures) == len(self.errors) == 0
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"Indicates that the tests should be aborted"
|
|
|
|
self.shouldStop = True
|
|
|
|
|
|
|
|
def _exc_info_to_string(self, err, test):
|
|
|
|
"""Converts a sys.exc_info()-style tuple of values into a string."""
|
|
|
|
exctype, value, tb = err
|
|
|
|
# Skip test runner traceback levels
|
|
|
|
while tb and self._is_relevant_tb_level(tb):
|
|
|
|
tb = tb.tb_next
|
|
|
|
if exctype is test.failureException:
|
|
|
|
# Skip assert*() traceback levels
|
|
|
|
length = self._count_relevant_tb_levels(tb)
|
|
|
|
return ''.join(traceback.format_exception(exctype, value, tb, length))
|
|
|
|
return ''.join(traceback.format_exception(exctype, value, tb))
|
|
|
|
|
|
|
|
def _is_relevant_tb_level(self, tb):
|
2010-03-21 21:06:30 -03:00
|
|
|
return '__unittest' in tb.tb_frame.f_globals
|
2009-07-19 17:18:21 -03:00
|
|
|
|
|
|
|
def _count_relevant_tb_levels(self, tb):
|
|
|
|
length = 0
|
|
|
|
while tb and not self._is_relevant_tb_level(tb):
|
|
|
|
length += 1
|
|
|
|
tb = tb.tb_next
|
|
|
|
return length
|
|
|
|
|
|
|
|
def __repr__(self):
|
2010-02-22 19:28:32 -04:00
|
|
|
return ("<%s run=%i errors=%i failures=%i>" %
|
2009-07-19 17:18:21 -03:00
|
|
|
(util.strclass(self.__class__), self.testsRun, len(self.errors),
|
2010-02-22 19:28:32 -04:00
|
|
|
len(self.failures)))
|