Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

doctest annoyance/puzzle

1 view
Skip to first unread message

Steven D'Aprano

unread,
Sep 5, 2010, 11:30:29 AM9/5/10
to
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:

>>> from module import make_spam
>>> make_spam(3)
'spam spam spam'

If you ask for too little spam, you get a bonus:

>>> make_spam(-3)
'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:

>>> it = iter(make_spam(5).split())
>>> next(it)
'spam'

text goes here

>>> next(it)
'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?


--
Steven

Stefan Schwarzer

unread,
Sep 5, 2010, 12:41:15 PM9/5/10
to
Hi Steven,

On 2010-09-05 17:30, Steven D'Aprano wrote:
> 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

Steven D'Aprano

unread,
Sep 5, 2010, 7:06:29 PM9/5/10
to
On Sun, 05 Sep 2010 18:41:15 +0200, Stefan Schwarzer wrote:

> Hi Steven,
>
> On 2010-09-05 17:30, Steven D'Aprano wrote:
>> 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.


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 :(


--
Steven

0 new messages