[TIP] TestCase.debug doesn't run tearDown in the event of an error?

Michael Foord fuzzyman at voidspace.org.uk
Wed Jul 28 07:02:52 PDT 2010

On 28/07/2010 14:59, Chris Withers wrote:
> Michael Foord wrote:
>>> from unittest import TestCase
>>> class Tests(TestCase):
>>>   def setUp(self):
>>>     print "setUp"
>>>   def tearDown(self):
>>>     print "tearDown"
>>>   def test_method(self):
>>>     raise ValueError('foo')
>>> tests = Tests('test_method')
>>> tests.debug()
>>> When test_method doesn't raise an exception, the tearDown is run.
>>> However, in the above example, the tearDown is not run.
>>> This is at odds with the normal unit test running experience.
>>> Why does debug behave differently in this respect?
>>> If it helps, this is on Python 2.5.2.
>> This was a design decision when TestCase.debug was first implemented 
>> - it simply stops on exceptions.
> Okay, the above is simplified from a unit test that tests a base test, 
> so the whole test case definition and call to debug are actually 
> inside a method of a test case.
> Is there a better method that debug I should be using that more 
> carefully mimics the behaviour of real test case use? ie: throws any 
> exceptions that occur in a test case, but runs both setUp and tearDown 
> even when they occur?

Well, you could just call it yourself.

test = TheTestCase('methodName')
except Exception:

A bit ungainly, sorry. I'm not really a fan of the debug method *anyway* 
but calling the tearDown on fail / exception would be a big semantic change.


> cheers,
> Chris


More information about the testing-in-python mailing list