[TIP] cleanUp for unittest

Michael Foord fuzzyman at voidspace.org.uk
Sat Apr 4 09:23:22 PDT 2009


I've written a patch that implements this:

http://bugs.python.org/issue5679

Comments on code or supporting comments on the issue welcomed. ;-)

Michael

Scott David Daniels wrote:
> Kumar McMillan wrote:
>> ... .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())
>>   
> 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.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.
>
> --Scott David Daniels
> Scott.Daniels at Acm.Org


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog





More information about the testing-in-python mailing list