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

Brett Cannon brett at python.org
Sat Jun 30 13:25:49 PDT 2007

There was an email to python-3000 recently from someone saying that we
should convert all unit tests for Python and its standard library over
to doctest.  That made me realize that I have never written a single
doctest in my life.  I started out learning unittest back in the day
and I have had issues with doctests back in the day where a very minor
change in output broke a bazillion unit tests.

But I am willing to give a doctest a shot.  To learn it I have a need
to write some regression tests for __import__.  But because it is
testing something that has a lot of file I/O I have a need to
temporarily create some files, run the tests, and then delete the
created files.

I read about the Unittest API, but I would like to be able to specify
what files to create on a per-test basis.  Normally I would use a
try/finally or develop a context manager, but that would suppress the
output from expression evaluation and thus kill the testing (if I
understand how doctest works).

So what is the best way to approach this?


More information about the testing-in-python mailing list