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

  • Thread starter Christopher Corbell
  • Start date
C

Christopher Corbell

This question pertains to PyUnit, esp. unittest.TestCase subclasses.

Does anyone know of a way from within the TestCase tearDown() method
to determine whether the current test succeeded or not?

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

This is somewhat related to some earlier threads in this group about
using fixtures across test invocations. I'm functionally testing a
system that uses a database. Successful tests are guaranteed to
restore the system's data fixtures to initial state, but when tests
fail or error-out we want to do a brute-force re-initialization of the
server so downstream tests can continue. This re-initialization takes
time so we don't want to do it in every tearDown(), just on error/
failure tearDown()'s.

TIA,
Chris
 
D

Duncan Booth

Christopher Corbell said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top