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

Jim Fulton jim at zope.com
Mon Jul 2 12:21:21 PDT 2007


On Jul 2, 2007, at 12:24 AM, Titus Brown 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.
> ->
> -> This just might be a situation where unittest is just a better fit.
>
> You might be right.  I wonder if the zope doctest guarantees that the
> teardown is called?

I won't repeat what Benji said about the setUp/tearDown keyword  
arguments.

It's important to note that *all* examples in a doctest string  
(docstring or text file) are executed.  This mean that if you choose  
to put set up and tear down code in your doctest, you are guaranteed  
that they will run, because all example code is run.

Jim

--
Jim Fulton			mailto:jim at zope.com		Python Powered!
CTO 				(540) 361-1714			http://www.python.org
Zope Corporation	http://www.zope.com		http://www.zope.org






More information about the testing-in-python mailing list