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

Working...