[TIP] Best way to do setup/teardown on a per-test basis using doctest?

Benji York benji at benjiyork.com
Mon Jul 2 06:05:31 PDT 2007

Brett Cannon wrote:
> I have a need to temporarily create some files, run the tests, and
> then delete the created files.

I'm not sure I understand what you're trying to accomplish, so I'll take
a stab at it and we can iterate from there.

It sounds like you want to create some source files in a particular
arrangement, call the import machinery, and then see how it interprets
the on-disk layout.  One approach would be to create a temporary
directory in your test setup file and pass that into the doctest (in the
globals).  You could then have a tear down function that removes the
directory when done.  If you expect each section of the doctest to clean
up after itself the tear down function could issue a warning or throw an
exception if the directory is non-empty.
Benji York

More information about the testing-in-python mailing list