print unittest errors immediately

Jeremy Hylton jeremy at alum.mit.edu
Mon Jan 21 16:21:14 EST 2002


I've wasted a lot of time waiting for unittests to finish running so
that I can see a traceback raised during the first test.  As often as
not, I'll see many errors all caused by the same bug.  To avoid
wasting so much time, I wrote a little extension to the TextTestRunner
that prints the error immediately instead of at the end.

If you've frittered away hours watching dots march across your screen
waiting for the traceback, I hope you'll find this little bit of code
helpful.

Jeremy

import traceback
import unittest

class ImmediateTestResult(unittest._TextTestResult):

    def _print_traceback(self, msg, err):
        if self.showAll or self.dots:
            self.stream.writeln("\n")
            
        self.stream.writeln(msg)
        self.stream.writeln(''.join(traceback.format_exception(*err)))


    def addError(self, test, err):
        self._print_traceback("Error in test %s" % test, err)

    def addFailure(self, test, err):
        self._print_traceback("Failure in test %s" % test, err)

class ImmediateTestRunner(unittest.TextTestRunner):

    def _makeResult(self):
        return ImmediateTestResult(self.stream, self.descriptions,
	                           self.verbosity)





More information about the Python-list mailing list