[TIP] cleanUp for unittest
fuzzyman at voidspace.org.uk
Sat Apr 4 09:23:22 PDT 2009
I've written a patch that implements this:
Comments on code or supporting comments on the issue welcomed. ;-)
Scott David Daniels wrote:
> Kumar McMillan wrote:
>> ... .The idea as I understand it is that you would build the list of
>> up functions dynamically. I could be wrong though as I don't use
>> Trial much.
>> def setUp(self):
>> self.db = ScratchDb()
>> self.cleanUp.append(lambda: self.db.teardown())
>> self.tmp = TempIO()
>> self.cleanUp.append(lambda: self.tmp.destroy())
> Here is where I have to rant slightly (although I agree with Kumar):
> _never_ write "lambda: function()" -- that "complexification."
> Instead, write "function". The idea of a function does not mean you
> cannot use a method: "obj.method" is a function which calls a method
> on an object. This is one of the great things about Python!
> So, the above should be:
> def setUp(self):
> self.db = ScratchDb()
> self.tmp = TempIO()
> 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.
> --Scott David Daniels
> Scott.Daniels at Acm.Org
More information about the testing-in-python