[TIP] cleanUp for unittest
Kumar McMillan
kumar.mcmillan at gmail.com
Fri Apr 3 10:33:38 PDT 2009
On Fri, Apr 3, 2009 at 12:16 PM, Michael Foord
<fuzzyman at voidspace.org.uk> wrote:
>>
>> Doesn't matter to me, but I'm wondering... why a list of functions?
>>
>> --titus
>>
> I should have listed the relevant issue: http://bugs.python.org/issue5538
>
> The idea is that as you allocate resources that need cleaning up, you
> push the corresponding clean up function onto the list. It is what trial
> uses.
>
> It sounds like a very simple system to me.
[one day I'm going to hack into Titus' mailman and set default reply
to "all." you've been warned!]
The idea as I understand it is that you would build the list of clean
up functions dynamically. I could be wrong though as I don't use
Trial much.
E.G.
def setUp(self):
self.db = ScratchDb()
self.db.setup()
self.cleanUp.append(lambda: self.db.teardown())
self.tmp = TempIO()
self.cleanUp.append(lambda: self.tmp.destroy())
vs. one cleanUp method, which could get very ugly with all the if
statements you would need to ensure it is safe to clean up a resource
that may or may not exist:
def cleanUp(self):
if hasattr(self, 'db'):
self.db.teardown()
if hasattr(self, 'tmp'):
self.tmp.destroy()
the problem cleanUp solves that tearDown cannot currently solve is,
for example, this scenario : self.db.setup() succeeds but TempIO()
does not. In today's world tearDown() would not get called so you
would be left with a database that was never torn down.
I am a big +1 on this feature.
More information about the testing-in-python
mailing list