Index: unittest.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/unittest.py,v retrieving revision 1.23 diff -c -r1.23 unittest.py *** unittest.py 4 Apr 2003 22:56:41 -0000 1.23 --- unittest.py 17 Apr 2003 15:27:11 -0000 *************** *** 65,70 **** --- 65,78 ---- def _strclass(cls): return "%s.%s" % (cls.__module__, cls.__name__) + def _traceback_len(tb): + "Return the number of frames in a traceback" + i = 1 + while tb.tb_next is not None: + tb = tb.tb_next + i += 1 + return i + class TestResult: """Holder for test result information. *************** *** 94,105 **** """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))) 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))) def addSuccess(self, test): "Called when a test has completed successfully" --- 102,113 ---- """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, True))) 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, False))) def addSuccess(self, test): "Called when a test has completed successfully" *************** *** 113,127 **** "Indicates that the tests should be aborted" self.shouldStop = 1 ! def _exc_info_to_string(self, err): ! """Converts a sys.exc_info()-style tuple of values into a string.""" ! return string.join(traceback.format_exception(*err), '') def __repr__(self): return "<%s run=%i errors=%i failures=%i>" % \ (_strclass(self.__class__), self.testsRun, len(self.errors), len(self.failures)) class TestCase: """A class whose instances are single test cases. --- 121,151 ---- "Indicates that the tests should be aborted" self.shouldStop = 1 ! def _exc_info_to_string(self, err, is_error=False): ! """Converts a sys.exc_info()-style tuple of values into a ! string. ! ! If is_error is true, a full traceback is returned, otherwise ! the innermost frame is not included because it refers to a ! failxxx() method. ! """ ! if is_error: ! info = traceback.format_exception(*err) ! return "".join(info) ! else: ! etype, evalue, etb = err ! info = traceback.format_exception(etype, evalue, etb, ! _traceback_len(etb)-1)[1:] ! lines = info[-1:] + info[:-1] ! return "".join(lines) def __repr__(self): return "<%s run=%i errors=%i failures=%i>" % \ (_strclass(self.__class__), self.testsRun, len(self.errors), len(self.failures)) + class TestFailed(Exception): + pass class TestCase: """A class whose instances are single test cases. *************** *** 149,155 **** # the instance's assertion methods fail; test methods raising this # exception will be deemed to have 'failed' rather than 'errored' ! failureException = AssertionError def __init__(self, methodName='runTest'): """Create an instance of the class that will use the named test --- 173,179 ---- # the instance's assertion methods fail; test methods raising this # exception will be deemed to have 'failed' rather than 'errored' ! failureException = TestFailed def __init__(self, methodName='runTest'): """Create an instance of the class that will use the named test *************** *** 279,288 **** callableObj(*args, **kwargs) except excClass: return else: ! if hasattr(excClass,'__name__'): excName = excClass.__name__ ! else: excName = str(excClass) ! raise self.failureException, excName def failUnlessEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the '==' --- 303,326 ---- callableObj(*args, **kwargs) except excClass: return + except Exception, detail: + etype, evalue, etb = sys.exc_info() + err = traceback.format_exception_only(etype, evalue)[-1] + raise self.failureException, "wrong exception, expected %s\ngot: %r" % \ + (self._exc_names(excClass), err.strip()) else: ! raise self.failureException, "no exception, expected %s" % self._exc_names(excClass) ! ! def _exc_names(self, excspec): ! # format excspec into a nice string ! try: ! n = len(excspec) ! except TypeError: # not a sequence ! return excspec.__name__ ! names = [cls.__name__ for cls in excspec] ! if n > 2: ! return ", ".join(names[:-1]) + ", or %s" % names[-1] ! return ", ".join(names) def failUnlessEqual(self, first, second, msg=None): """Fail if the two objects are unequal as determined by the '=='