Running DocTest on Strings

Discussion in 'Python' started by notanotheridiot, Jun 29, 2006.

  1. Hi,
    I have two strings - a docstring containing doctests and a code string
    containing code to be tested with those doctests. I've been trying for
    a day now to run the test without concatenating the two strings,
    adding:

    import doctest
    doctest.testmod

    to the bottom, writing it all to a file and executing it using popen().

    There must be some way of doing this without writing to a temporary
    file, any ideas?

    thanks in advance,

    johannes Woolard
    notanotheridiot, Jun 29, 2006
    #1
    1. Advertising

  2. notanotheridiot

    Paddy Guest

    notanotheridiot wrote:
    > Hi,
    > I have two strings - a docstring containing doctests and a code string
    > containing code to be tested with those doctests. I've been trying for
    > a day now to run the test without concatenating the two strings,
    > adding:
    >
    > import doctest
    > doctest.testmod
    >
    > to the bottom, writing it all to a file and executing it using popen().
    >
    > There must be some way of doing this without writing to a temporary
    > file, any ideas?
    >
    > thanks in advance,
    >
    > johannes Woolard

    Create the concatenated string then exec it?

    - Pad.
    Paddy, Jun 29, 2006
    #2
    1. Advertising

  3. Paddy wrote:
    > notanotheridiot wrote:
    > > Hi,
    > > I have two strings - a docstring containing doctests and a code string
    > > containing code to be tested with those doctests. I've been trying for
    > > a day now to run the test without concatenating the two strings,
    > > adding:
    > >
    > > import doctest
    > > doctest.testmod
    > >
    > > to the bottom, writing it all to a file and executing it using popen().
    > >
    > > There must be some way of doing this without writing to a temporary
    > > file, any ideas?
    > >
    > > thanks in advance,
    > >
    > > johannes Woolard

    > Create the concatenated string then exec it?


    I know from experience that this approach does not work. When you do
    that, the entire module from which it is run turns out to be scanned
    for doctests - not only the string being executed by exec. However, I
    understand that Johannes found a solution.

    André

    >
    > - Pad.
    =?iso-8859-1?B?QW5kcuk=?=, Jun 29, 2006
    #3
  4. André wrote:
    > Paddy wrote:
    > > notanotheridiot wrote:
    > > > Hi,
    > > > I have two strings - a docstring containing doctests and a code string
    > > > containing code to be tested with those doctests. I've been trying for
    > > > a day now to run the test without concatenating the two strings,
    > > > adding:
    > > >
    > > > import doctest
    > > > doctest.testmod
    > > >
    > > > to the bottom, writing it all to a file and executing it using popen().
    > > >
    > > > There must be some way of doing this without writing to a temporary
    > > > file, any ideas?
    > > >
    > > > thanks in advance,
    > > >
    > > > johannes Woolard

    > > Create the concatenated string then exec it?

    >
    > I know from experience that this approach does not work. When you do
    > that, the entire module from which it is run turns out to be scanned
    > for doctests - not only the string being executed by exec. However, I
    > understand that Johannes found a solution.
    >
    > André
    >
    > >
    > > - Pad.


    Got it, I've blogged about it here:
    http://pytute.blogspot.com/2006/06/say-no-to-temporary-files.html
    Basically the trick is to pass the docstring ino the exec environment
    as a variable and use the advanced doctest api to get a doctest.

    Johannes
    notanotheridiot, Jun 30, 2006
    #4
    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. Steven Taschuk

    Re: Simulte user input using doctest

    Steven Taschuk, Jun 27, 2003, in forum: Python
    Replies:
    0
    Views:
    1,202
    Steven Taschuk
    Jun 27, 2003
  2. Paul M

    ps2, IDLE, and doctest

    Paul M, Oct 17, 2003, in forum: Python
    Replies:
    0
    Views:
    395
    Paul M
    Oct 17, 2003
  3. Replies:
    0
    Views:
    260
  4. Pierre Rouleau
    Replies:
    14
    Views:
    503
    Pierre Rouleau
    Feb 27, 2004
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    729
    Malcolm
    Jun 24, 2006
Loading...

Share This Page