diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-07-19 21:01:52 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-07-19 21:01:52 (GMT) |
commit | bed7d04fedd800a8d3bd8a7c9c3ff823e365c236 (patch) | |
tree | f7e20f085f4c4a6a4cc0227ab16aa73d1dd7dc8e /Lib/unittest/result.py | |
parent | c4296d1edc1f05f96cc7b6b3e1c3df9dd596d5f4 (diff) | |
download | cpython-bed7d04fedd800a8d3bd8a7c9c3ff823e365c236.zip cpython-bed7d04fedd800a8d3bd8a7c9c3ff823e365c236.tar.gz cpython-bed7d04fedd800a8d3bd8a7c9c3ff823e365c236.tar.bz2 |
Merged revisions 74095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74095 | benjamin.peterson | 2009-07-19 15:18:21 -0500 (Sun, 19 Jul 2009) | 1 line
split unittest.py into a package
........
Diffstat (limited to 'Lib/unittest/result.py')
-rw-r--r-- | Lib/unittest/result.py | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/Lib/unittest/result.py b/Lib/unittest/result.py new file mode 100644 index 0000000..3e32eb0 --- /dev/null +++ b/Lib/unittest/result.py @@ -0,0 +1,113 @@ +"""Test result object""" + +import traceback + +from . import util + + +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. + """ + def __init__(self): + self.failures = [] + self.errors = [] + self.testsRun = 0 + self.skipped = [] + self.expectedFailures = [] + self.unexpectedSuccesses = [] + self.shouldStop = False + + def startTest(self, test): + "Called when the given test is about to be run" + self.testsRun = self.testsRun + 1 + + def startTestRun(self): + """Called once before any tests are executed. + + See startTest for a method called before each test. + """ + + def stopTest(self, test): + "Called when the given test has been run" + pass + + def stopTestRun(self): + """Called once after all tests are executed. + + See stopTest for a method called after each test. + """ + + 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))) + + 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))) + + 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): + globs = tb.tb_frame.f_globals + is_relevant = '__name__' in globs and \ + globs["__name__"].startswith("unittest") + del globs + return is_relevant + + 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): + return "<%s run=%i errors=%i failures=%i>" % \ + (util.strclass(self.__class__), self.testsRun, len(self.errors), + len(self.failures)) |