[TIP] cleanUp for unittest

Douglas Philips dgou at mac.com
Sun Apr 5 18:02:47 PDT 2009

On or about 2009 Apr 4, at 12:23 PM, Scott David Daniels indited:
> So, the above should be:
>   def setUp(self):
>        self.db = ScratchDb()
>        self.db.setup()
>        self.cleanUp.append(self.db.teardown)
>        self.tmp = TempIO()
>        self.cleanUp.append(self.tmp.destroy)
> For those who are worried about "How do I throw in a few args?":
>    import functools as ft
>    ...
>         self.cleanUp.append(ft.partial(function, arg0, arg1))
>    ...
>> I am a big +1 on this feature.
> Having implemented this too many times already in tests, I am tempted
> to be +2, but I will bow to community pressure and only vote +1.

Ugh, well, if I were going to do that kind of thing (and I admit that  
I like the cleanup LIFO stack idea a lot), I would have some utility  
   def setUp(self):
        self.db = self.setupScratchDb()
        self.tmp = self.setupTempIO()
# save the manual self.cleanUp.append() calls for those cases where  
they really are one-offs.

I've also done things in other non-test code where the creating  
function returns a tuple:
new_thing, clean_up_thunk =  

Because, really, it is an encapsulation failure if the code asking for  
a new "thing" has to know how to clean it up.


More information about the testing-in-python mailing list