doctest annoyance/puzzle

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

I'm writing a "literate programming" document, example.txt, which mixes
text and code in ReST format:


This is my excellent module for making spam. It has
one public function, ``make_spam``, which takes a
single argument for how much spam to make:
'spam spam spam'

If you ask for too little spam, you get a bonus:
'spam spam spam GLORIOUS SPAM!!!'



I run the doctests with:

python2.6 -m doctest examples.txt

and the first example passes, but the second fails with NameError:
make_spam not defined.

I have a work-around, I do an import at the start of *every* doctest
block, but that's ugly. More complex (realistic) examples sometimes need
setup code, which I'd like to survive from one block to the next:
'spam'

text goes here
'spam'


but of course that also fails. This surprises me, because doctests in
functions or classes can mix descriptive text between tests without this
problem.

Am I doing something wrong, or is doctest fundamentally broken when
executing tests in a text file?
 
S

Stefan Schwarzer

Hi Steven,

I run the doctests with:

python2.6 -m doctest examples.txt

and the first example passes, but the second fails with NameError:
make_spam not defined.

I run my doctests by calling

doctest.testfile(filename)

for each file in a loop. This way, names and their
associated objects survice from one code block to the next.

I just read that the way you use doctest should behave the
same, according to the documentation. In case of a text file
I just tested, it does; all tests pass despite the text
snippets between the code blocks.

What do you get if you test your text file by explicitly
calling doctest.testfile?

Stefan
 
S

Steven D'Aprano

Hi Steven,



I run my doctests by calling

doctest.testfile(filename)

for each file in a loop. This way, names and their associated objects
survice from one code block to the next.


That's fine if you're calling it from Python, but I want a way to run the
tests from the shell, and the advertised way to do that is by calling
doctest.py as a script. The module calls testfile, so it should work.

And now it's working for me. I have *no idea* what happened there... it
wasn't working for me before, and now it is.

Sorry for the noise :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top