By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

to Doctest as SystemExit is to Python

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
(((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.