[TIP] setUp and tearDown behavior

Mark Roddy markroddy at gmail.com
Wed Jan 20 16:47:06 PST 2010


On Tue, Jan 19, 2010 at 9:06 AM, Michael Foord
<fuzzyman at voidspace.org.uk> wrote:
> 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.
>
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>


Regarding adding class wide setup and teardown functionality to
unittest, I emailed Michael regarding this possibility and per his
suggestion I've emailed python-ideas to discuss the semantics of such
functionality for anyone who has interest/opinions in the issue.

-Mark



More information about the testing-in-python mailing list