Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > doctest annoyance/puzzle

Reply
Thread Tools

doctest annoyance/puzzle

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-05-2010
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
 
Reply With Quote
 
 
 
 
Stefan Schwarzer
Guest
Posts: n/a
 
      09-05-2010
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-05-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: doctest Tim Peters Python 0 04-05-2004 12:47 AM
Problem with mixing doctest with gettext _() Pierre Rouleau Python 14 02-27-2004 10:29 PM
NEWBIE: doctest question engsolnom@ipns.com Python 0 01-05-2004 09:47 PM
ps2, IDLE, and doctest Paul M Python 0 10-17-2003 07:03 PM
Re: Simulte user input using doctest Steven Taschuk Python 0 06-27-2003 01:57 PM



Advertisments