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

Atul Varma varmaa at gmail.com
Sun Jul 1 21:47:04 PDT 2007


On 7/1/07, Brett Cannon <brett at python.org> wrote:
> But how do I guarantee that the teardown is called?  If do something like::
>
> """Here are some tests
>   >>> with setup():
>   ...   test1
>   ...   test2
>
> """
>
> That won't test anything from my understanding of doctest as that
> would be executed as a single statement and not a bunch of indivudal
> expressions to verify the output.

I'm not sure if this suggestion is too obvious--it's certainly not
particularly ideal--but have you considered using the setUp and
tearDown keyword arguments to the DocFileSuite/DocTestSuite functions?
 They're not very granular, as they're called around each file that's
doctested, but using this mechanism does guarantee that the teardown
is called.  But perhaps more granular control is what you meant when
you wrote that you wanted to be able to specify what files to create
on a per-test basis; I guess the definition of "test" is slightly
ambiguous here, since doctest seems to treat an entire file as a
single test, yet we tend think of a doctest file as containing
multiple tests.

- Atul



More information about the testing-in-python mailing list