summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Purcell <steve@pythonconsulting.com>2001-09-06 08:24:40 (GMT)
committerSteve Purcell <steve@pythonconsulting.com>2001-09-06 08:24:40 (GMT)
commit7b0657027f04ac0367908ee0474f6d762366e4bb (patch)
tree617992f9bbf5eedb4f2f42ea7f823ce7d3a6ce17
parent387c547fd38d321e42bf202c166e9403e7ee9b61 (diff)
downloadcpython-7b0657027f04ac0367908ee0474f6d762366e4bb.zip
cpython-7b0657027f04ac0367908ee0474f6d762366e4bb.tar.gz
cpython-7b0657027f04ac0367908ee0474f6d762366e4bb.tar.bz2
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)
-rw-r--r--Lib/unittest.py25
1 files 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: