5.3.8 Getting Extended Error Information

Some applications can make use of more error information (for example, an integrated development environment, or IDE). Such an application can retrieve supplemental information about errors and failures by using an alternate TestResult implementation, and extending the defaultTestResult() method of the TestCase class to provide it.

Here is a brief example of a TestResult subclass which stores the actual exception and traceback objects. (Be aware that storing traceback objects can cause a great deal of memory not to be reclaimed when it otherwise would be, which can have effects that affect the behavior of the tests.)

import unittest

class MyTestCase(unittest.TestCase):
    def defaultTestResult(self):
        return MyTestResult()

class MyTestResult(unittest.TestResult):
    def __init__(self):
        self.errors_tb = []
        self.failures_tb = []

    def addError(self, test, err):
        self.errors_tb.append((test, err))
        unittest.TestResult.addError(self, test, err)

    def addFailure(self, test, err):
        self.failures_tb.append((test, err))
        unittest.TestResult.addFailure(self, test, err)

Tests written using MyTestCase as the base class, instead of TestCase, will allow tools to extract additional information from the results object.

See About this document... for information on suggesting changes.