467,222 Members | 1,416 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,222 developers. It's quick & easy.

iPython and doctest

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

Apr 12 '06 #1
  • viewed: 1664
Share:
1 Reply
Michael Tobis wrote:
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)


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
ro*********@gmail.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

Apr 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Pierre Rouleau | last post: by
2 posts views Thread by Alan G Isaac | last post: by
2 posts views Thread by Michele Simionato | last post: by
5 posts views Thread by Michele Simionato | last post: by
1 post views Thread by Runsun Pan | last post: by
reply views Thread by Michael Tobis | last post: by
reply views Thread by Fernando Perez | last post: by
reply views Thread by Eric Mahurin | last post: by
6 posts views Thread by Bzyczek | last post: by
reply views Thread by Adict | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.