[Zope-Coders] Help with Unit Testing

Guido van Rossum guido@python.org
Tue, 13 Aug 2002 09:07:08 -0400


> > > You could create the skins in the tempfile area (though you'll have to be
> > > careful to *not* delete immediately after creation, which is a standard
> > > Unixism.)
> >
> > Huh? Lost me on the bit in brackets...
> 
> I think he meant that a common convention in unix programs is
> to open the file in /tmp and then immediatly "delete it" (ie: unlink
> it).  Because the file is still open, the creating program gets
> to use it, but it is inivisible to the rest of the system.  And
> as soon as the file is closed for whatever reason (close() call,
> program crash) the disk blocks are freed for reuse.
> 
> So, he's saying, don't write your code that way in this case, presumably
> because the whole reason you need the files in the first place
> requires that they be visible on the file system during the test.

You should use tempfile.TemporaryFile to create a file with that
"disappearing" property; on Unix, this uses the standard trick, while
on other systems it leaves the file around and returns a wrapper whose
destructor deletes the file.  (Too bad when your program crashes
though.)

--Guido van Rossum (home page: http://www.python.org/~guido/)