iPython and doctest

M

Michael Tobis

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.
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
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
 
R

Robert Kern

Michael said:
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.

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

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
(e-mail address removed)

"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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top