From 7b0657027f04ac0367908ee0474f6d762366e4bb Mon Sep 17 00:00:00 2001 From: Steve Purcell Date: Thu, 6 Sep 2001 08:24:40 +0000 Subject: Changed TestResult to store only the text representation of an error. This patch is similar to that proposed by Jeremy. The proposed patch altered the interface of TestResult such that it would be passed the error information as a string rather than an exc_info() tuple. The implemented change leaves the interface untouched so that TestResults are still passed the tracebacks, but stor them in stringified form for later reporting. Notes: - Custom subclasses of TestResult written by users should be unaffected. - The existing 'unittestgui.py' will still work with this module after the change. - Support can later be added to pop into the debugger when an error occurs; this support should be added to a TestRunner rather than to TestCase itself, which this change will enable. (Jeremy, Fred, Guido: Thanks for all the feedback) --- Lib/unittest.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Lib/unittest.py b/Lib/unittest.py index 480e825..9aa79b3 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -16,7 +16,7 @@ Simple usage: def testAdd(self): ## test method names begin 'test*' self.assertEquals((1 + 2), 3) self.assertEquals(0 + 1, 1) - def testMultiply(self); + def testMultiply(self): self.assertEquals((0 * 10), 0) self.assertEquals((5 * 8), 40) @@ -67,8 +67,8 @@ class TestResult: 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 a - tuple of values as returned by sys.exc_info(). + contain tuples of (testcase, exceptioninfo), where exceptioninfo is the + formatted traceback of the error that occurred """ def __init__(self): self.failures = [] @@ -85,12 +85,15 @@ class TestResult: pass def addError(self, test, err): - "Called when an error has occurred" - self.errors.append((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))) def addFailure(self, test, err): - "Called when a failure has occurred" - self.failures.append((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" @@ -104,6 +107,10 @@ class TestResult: "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(apply(traceback.format_exception, err), '') + def __repr__(self): return "<%s run=%i errors=%i failures=%i>" % \ (self.__class__, self.testsRun, len(self.errors), @@ -575,9 +582,7 @@ class _TextTestResult(TestResult): self.stream.writeln(self.separator1) self.stream.writeln("%s: %s" % (flavour,self.getDescription(test))) self.stream.writeln(self.separator2) - for line in apply(traceback.format_exception, err): - for l in string.split(line,"\n")[:-1]: - self.stream.writeln("%s" % l) + self.stream.writeln("%s" % err) class TextTestRunner: -- cgit v0.12