[TIP] setUp and tearDown behavior

Michael Foord fuzzyman at voidspace.org.uk
Tue Jan 19 06:06:12 PST 2010


On 19/01/2010 13:53, Olemis Lang wrote:
> [snip...]
>> In case this is expected, is there a way to do it just once for all the
>> methods?
>>
>>      
> Using standard `unittest` module ? well ... no.
>
>    
Well - you can make setup *effectively* only run the first time using a 
class attribute:


class SomeTest(unittest.TestCase):
doneSetup = False
def setUp(self):
if not self.__class__.doneSetup:
self.__class__.doneSetup = True
self.setUpClass()
...

Making tearDown only run the *last* time is a bit harder (you could hack 
it in with a counter in setUp and only execute tearDown when the counter 
== number of test methods). Builtin support for class level fixtures in 
unittest would be good, even if they are ripe for abuse (shared state 
between fixtures).

All the best,

Michael Foord


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog

READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.





More information about the testing-in-python mailing list