[TIP] cleanUp for unittest

Jonathan Lange jml at mumak.net
Fri Apr 3 16:21:04 PDT 2009

On Sat, Apr 4, 2009 at 4:08 AM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
> Hello all,
> There is a feature request for unittest that we add a 'cleanUp' list.
> This is a list of functions to call on exit of a test that can be added
> to in setUp or during test execution. The difference between this and
> tearDown is that if anything is in the cleanUp list they will be called
> *even* if setUp fails (which normally means tearDown is skipped).
> Is there consensus that adding this to unittest is a good thing? If
> there is then I will just do it...

Bazaar, Twisted and Launchpad all have implementations of this in
their test suites. We all like it a lot.

testtools (at https://launchpad.net/testtools) also has an
implementation which comes complete with unit tests and is basically
ready to go into trunk. Please feel free to copy as much of that as
you'd like.

I tried adding it to Python but became embroiled in a controversy
similar to this one.

The signature is:
    def addCleanup(self, function, *args, **kwargs):

It's very, very, very useful for dynamic allocation of resources in tests.


More information about the testing-in-python mailing list