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

Duncan Booth duncan.booth at invalid.invalid
Tue Mar 6 03:34:00 EST 2007


"Christopher Corbell" <chriscorbell at gmail.com> 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):
        try:
            f(self)
            self.testResult = 'pass'
        except AssertionError:
            self.testResult = 'fail'
            raise
        except:
            self.testResult = 'error'
            raise
    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