to Doctest as SystemExit is to Python

P

p.lavarre

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
 
P

Peter Otten

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):
""" '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
 
P

p.lavarre

(((I thought I had sent this reply already, Google says No.)))
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.:
Traceback (most recent call last):
...
SystemExit
 

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,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top