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

Chris Withers chris at simplistix.co.uk
Wed Jul 28 06:59:06 PDT 2010


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?

cheers,

Chris

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk



More information about the testing-in-python mailing list