[TIP] cleanUp for unittest

C. Titus Brown ctb at msu.edu
Fri Apr 3 19:40:06 PDT 2009


On Fri, Apr 03, 2009 at 12:33:38PM -0500, Kumar McMillan wrote:
-> On Fri, Apr 3, 2009 at 12:16 PM, Michael Foord
-> <fuzzyman at voidspace.org.uk> wrote:
-> > 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.

[ ... ]

-> 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())

[ ... ]

-> 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.

Well, you've convinced me; that's very clean and neat.  +1.

I like the other syntax propounded on the list a bit better,

  self.cleanUp.append(self.tmp.destroy, (args, kwargs))

but that's just me.

cheers,
--titus

p.s.  I'm serious about this one.
-- 
C. Titus Brown, ctb at msu.edu



More information about the testing-in-python mailing list