[TIP] addCleanup for setUpClass()?
barry at python.org
Thu Aug 1 07:11:22 PDT 2013
On Aug 01, 2013, at 02:53 PM, Prasanna Santhanam wrote:
>A lot of our tests share resources created within setUpClass and it
>would be useful to override addCleanup to perform such garbage
>collection when setUpClass fails.
If you can use Python 3.3, or don't mind the external dependency on
contextlib2, then take a look at ExitStack
You can build up a fairly elaborate state, wrapped in a try/except. Then if
you get an exception during your setUpClass(), you'd just clear the stack to
reclaim your resources. Doing almost exactly the same thing in
tearDownClass() reclaims the resources in the case where everything succeeded.
cls._resources = ExitStack()
another = cls._resources.enter_context(some_context_manager())
Of course, the utility of ExitStack isn't limited to unittest. See also:
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 836 bytes
Desc: not available
More information about the testing-in-python