doctest annoyance/puzzle

Discussion in 'Python' started by Steven D'Aprano, Sep 5, 2010.

  1. 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
     
    Steven D'Aprano, Sep 5, 2010
    #1
    1. Advertising

  2. 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
     
    Stefan Schwarzer, Sep 5, 2010
    #2
    1. Advertising

  3. 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
     
    Steven D'Aprano, Sep 6, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PL
    Replies:
    0
    Views:
    2,653
  2. Replies:
    0
    Views:
    317
  3. Chris

    JavaHelp annoyance

    Chris, Apr 6, 2006, in forum: Java
    Replies:
    1
    Views:
    383
    Bjorn Abelli
    Apr 6, 2006
  4. rf

    annoyance question

    rf, Nov 16, 2003, in forum: HTML
    Replies:
    27
    Views:
    774
  5. David Baxter

    Javascript / IE6 Annoyance

    David Baxter, Apr 8, 2005, in forum: HTML
    Replies:
    2
    Views:
    506
    Csaba Gabor
    Apr 9, 2005
Loading...

Share This Page