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

Brett Cannon brett at python.org
Tue Jul 3 11:16:50 PDT 2007


On 7/2/07, Benji York <benji at benjiyork.com> wrote:
> 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.

Yep.

>  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.

At the test file level for the teardown, right?

>  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.

This could work.  The setup code would be per-test and could literally
just wipe the directory contents clean and set it up to how it needs
to be.  Then the teardown, at the doctest file level, would just
clobber the directory itself and anything contained within.

-Brett



More information about the testing-in-python mailing list