[TIP] cleanUp for unittest

Marius Gedminas marius at gedmin.as
Fri Apr 3 16:38:51 PDT 2009


On Fri, Apr 03, 2009 at 02:49:17PM -0400, Victoria G. Laidler wrote:
> Michael Foord wrote:
> >
> > There is no need for the lambdas in the above example.
> >
> >> and I'm dithering on whether I think it ought to be a list, as 
> >> proposed, or something else.
> >>
> >> My usual case for cleanup behavior is file deletion, with different 
> >> files to be deleted depending on how far I got. Would this work?
> >>
> >> self.cleanUp.append(os.unlink(somefile))
> >
> > You do need a lambda (or function) to do this however. :-)
> >
> > self.cleanUp.append(lambda: os.unlink(somefile))
> OK... what is the difference between the two? Just that the method takes 
> an argument? I'm still lambda-befuzzled, here.

Without the lambda the code would call os.unlink immediatelly and put
the returned value (which is None in this case) into the cleanup list.

With the lambda the code would create an unnamed function that calls
os.unlink, and put that function into the list.  os.unlink would only be
called with the rest of the registered cleanup functions.

Personally I'd prefer the other API mentioned in this thread:

    self.addCleanup(os.unlink, somefile)

It matches the pattern used by, e.g. the atexit module that was also
mentioned.

Marius Gedminas
-- 
I noticed that Open Source proponents using MacOS X have developed highly tuned
excuses, similar to those that smokers have about why cigarettes are good for
you.
        -- Miguel de Icaza,
           http://primates.ximian.com/~miguel/archive/2004/Aug-03.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20090404/6dc21e02/attachment.pgp 


More information about the testing-in-python mailing list