[TIP] cleanUp for unittest

Michael Foord fuzzyman at voidspace.org.uk
Fri Apr 3 10:21:09 PDT 2009

jason pellerin wrote:
> On Fri, Apr 3, 2009 at 1:10 PM, C. Titus Brown <ctb at msu.edu> wrote:
>> On Fri, Apr 03, 2009 at 06:08:58PM +0100, Michael Foord wrote:
>> -> 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...
>> ->
>> -> Should it be done before or after calling tearDown? I don't think it
>> -> matters so long as it is documented.
>> Doesn't matter to me, but I'm wondering... why a list of functions?
> +1 cleanUp, -1 list of functions. If "override this in a subclass" is
> good enough for setUp and tearDown, it's good enough for cleanUp. And
> as long as I'm spouting opinions, cleanUp should run last, after
> tearDown, since that gives you a chance to clean up after a failed
> tearDown too.

If cleanUp is just another method it offers little benefit over 
tearDown  other than the guarantee that it is run (and I won't be 
implementing it).

I think the list of functions technique is a very clean way of doing 
resource allocation / clean up within your setUp and tests.


> JP


More information about the testing-in-python mailing list