467,920 Members | 1,227 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

to Doctest as SystemExit is to Python

From: http://docs.python.org/lib/doctest-soapbox.html ...
Regression testing is best confined to dedicated objects or files ...
Can I somehow tell doctest that it's time to quit?

I ask because not all doctest examples are created equal. Some
failures are catastrophic, making all subsequent failures at least
uninteresting, and maybe painfully slow. Other failures are
negligible, making the print of any subsequent failure valuable.

So sys.exit() doesn't do what I mean: it raises SystemExit, but doctest
redefines the SystemExit exception to mean print a traceback, rather
than meaning to exit quietly after a noisily catastrophic failure,
exiting almost as if the doctest had passed.

And doctest.REPORT_ONLY_FIRST_FAILURE doesn't do what I mean: it prints
only the first failure, always, never the valuable subsequent failures.

Can doctest somehow be persuaded to do the right thing? That is, to
exit after a catastrophic failure, but not after a negligible failure,
and not after an independent failure?

In the last Python I shipped, to get the effect of exit after
catastrophe, I actually faked a run of doctest:

print 'Expected:'
print " '...'"
print 'Got:'
print " " + repr(result)
print '***Test Failed***'
sys.exit(-1)

Surely that's wrong? Somehow not idiomatic Python?

Surely I can somehow tell doctest that my code has just realised,
sorry, it is time to quit?

Thanks in advance, Pat LaVarre

Nov 10 '06 #1
  • viewed: 2068
Share:
2 Replies
p.*******@ieee.org wrote:
Can I somehow tell doctest that it's time to quit?
Hit Ctrl-C. Or raise a KeyboardInterrupt:

import sys

class ExitDoctest(KeyboardInterrupt):
pass

def f(what):
"""
>>f("alpha")
'alpha'
>>f("e")
'e'
>>f("x")
'x'
>>f("X")
'X'
>>f("beta")
'beta'
"""
if what == "e":
raise Exception
elif what == "x":
sys.exit()
elif what == "X":
raise ExitDoctest("You're in trouble")
return what

if __name__ == "__main__":
import doctest
try:
doctest.testmod()
except ExitDoctest, e:
print >sys.stderr, e

Peter
Nov 10 '06 #2
(((I thought I had sent this reply already, Google says No.)))
Can I somehow tell doctest that it's time to quit?
... doctest redefines ... SystemExit ...

Hit Ctrl-C. Or raise a KeyboardInterrupt:
Yes! Thank you!!! I see now,

Doctest exactly reverses the python -i experience:

doctest exits quietly if I raise KeyboardInterrupt.
doctest catches SystemExit and prints the traceback.

python -i exits quietly if I raise SystemExit.
python -i catches KeyboardInterrupt and prints the traceback.

Of course, to make the doctest work as sketched, I had to strip the
trailing blanks that my browser introduced and I had to expect
tracebacks for raise Exception and raise SystemExit, e.g.:
>>import sys
sys.exit()
Traceback (most recent call last):
...
SystemExit
>>>
Nov 10 '06 #3

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
3 posts views Thread by John J Lee | last post: by
reply views Thread by Steven Bethard | last post: by
reply views Thread by Eric Mahurin | last post: by
12 posts views Thread by thomas.guest | last post: by
6 posts views Thread by Bzyczek | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.