iPython and doctest

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Tobis

    #1

    iPython and doctest

    It appears that doctest does not work straightforward ly within iPython.


    I would like to be able to use doctest within a file conditionally, so
    that I can develop it within ipython and test it otherwise.

    It would seem that this would work:

    Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import testme
    >>> testme._test()
    >>>[/color][/color][/color]

    but it doesn't (the test above fails, but reports nothing)

    I am finding the documentation for doctest ironically impenetrable and
    **would** be interested in adding some examples and explanatory text to
    it, but I need to understand what is going on rather better than I do.

    Meanwhile I am settling for this:

    # testme.py

    def foo():
    """
    Should return 42
    [color=blue][color=green][color=darkred]
    >>> foo()[/color][/color][/color]
    42
    """

    return 43

    def _test():
    import doctest
    doctest.testmod ()

    if __name__ == "__main__":
    try:
    __IP # check if we are in iPython
    except:
    _test()
    print "ok"

    ####

    Then

    In [4]:!python testme.py

    works (reports the error) just fine! So I don't even have to bail out
    of iPython to run the tests.

    mt

  • Robert Kern

    #2
    Re: iPython and doctest

    Michael Tobis wrote:[color=blue]
    > It appears that doctest does not work straightforward ly within iPython.
    >
    > I would like to be able to use doctest within a file conditionally, so
    > that I can develop it within ipython and test it otherwise.
    >
    > It would seem that this would work:
    >
    > Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
    > [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
    > Type "help", "copyright" , "credits" or "license" for more information.
    >[color=green][color=darkred]
    >>>>import testme
    >>>>testme._tes t()
    >>>>[/color][/color]
    >
    > but it doesn't (the test above fails, but reports nothing)[/color]

    IPython replaces sys.displayhook (), the function that determines how an object
    gets printed when it is the result of an expression in the interactive
    interpreter. Consequently, if you write your doctests as if they were executed
    from the virgin interpreter, then you will get different values if you run the
    tests in IPython. And vice versa. Writing for IPython and executing for IPython
    may or may not work.

    --
    Robert Kern
    robert.kern@gma il.com

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    Working...