Any way to determine test success from inside tearDown()?

Duncan Booth duncan.booth at invalid.invalid
Tue Mar 6 09:34:00 CET 2007

"Christopher Corbell" <chriscorbell at> wrote:

> What I'm after is a specialized clean-up approach for error/failure
> cases.

How about decorating the tests with something which tracks the status? 
Something like (completely untested):

def track_failure(f):
    def test(self):
            self.testResult = 'pass'
        except AssertionError:
            self.testResult = 'fail'
            self.testResult = 'error'
    return test

and then either decorate the tests directly when you define then, or 
perhaps easier just iterate over all the methods in the test class and 
decorate anything beginning 'test_'.

Alternatively try subclassing (or monkey patching) TestResult: the 
TestResult has methods addSuccess, addFailure and addError which are 
called at a suitable point, with the test as an argument. You could set 
a flag in the test at that point and check it during the cleanup. If you 
subclass TextTestRunner and override _makeResult you can substitute in 
your own TestResult class at that point.

More information about the Python-list mailing list