iPython and doctest

Michael Tobis mtobis at gmail.com
Wed Apr 12 17:46:17 EDT 2006


It appears that doctest does not work straightforwardly 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.
>>> import testme
>>> testme._test()
>>>

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

   >>> foo()
   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




More information about the Python-list mailing list