[TIP] cleanUp for unittest

Kumar McMillan kumar.mcmillan at gmail.com
Fri Apr 3 12:57:27 PDT 2009

On Fri, Apr 3, 2009 at 2:28 PM, Olemis Lang <olemis at gmail.com> wrote:
> On Fri, Apr 3, 2009 at 2:00 PM, Kumar McMillan <kumar.mcmillan at gmail.com> wrote:
>> On Fri, Apr 3, 2009 at 1:45 PM, jason pellerin <jpellerin at gmail.com> wrote:
>>> Taking Michael's version of Kumar's example:
>>>> Yes - precisely; except in this particular example you would actually do:
>>>> def setUp(self):
>>>>    self.db = ScratchDb()
>>>>    self.db.setup()
>>>>    self.cleanUp.append(self.db.teardown)
>>>>    self.tmp = TempIO()
>>>>    self.cleanUp.append(self.tmp.destroy)

self.addCleanup(self.db.teardown), I like this the best.

this thread is a bit long now but the point of it was because unittest
does not make the unconditional clean up pattern easy.  You
essentially have to overload TestCase.runTest() to inject a
try/finally block with your own custom implementation of cleanUp().
Or use atexit.register() which only gets you half of the way there
(cleanup is deferred til the end which can be problematic for long
running test suites).

More information about the testing-in-python mailing list