467,162 Members | 934 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

Debugging doctest failures

Noob here. Just got into python a little over a week ago...

One of the (unique?) things I really like about python is the concept
of doctesting. But, now I want more! Here's what I'd like to see:

* easy debugging. As soon as there is a failure (unexpected exception
or mismatch), drop down into the debugger in a way to isolates the bug
down to the nearest test/example as much as possible.

* integrated with code coverage. I'd like to be targetting 100% code
coverage with doctests. With full (or a target) coverage, no
additional coverage data would be needed and otherwise something that
tells which lines don't have coverage would be good. A way to mark
code that you don't care about covering (typically error conditions -
at least initially) would also be good.

* whether stdout and/or sterr should be checked.
If anybody has any pointers to what I should be doing with doctest or
another framework I should be using (zope.testing?), I'd appreciate
it.

I made a crack at the first one above (easy debugging). Here is a
little script I came up with. You just give modules you want to
doctest as arguments. When a normal failure occurs, it will restart
the test (with verbose) and set traces before each example. When an
unexpected exception occurs, it pulls up the debugger with the
backtrace and continuing will restart the test just like a normal
failure. I've used this a bit and it allows me to find bugs very fast
without changing the code a bit for debug purposes. The
implementation below is quite hacky and tied to the implementation
within the doctest module.
#!/usr/bin/env python

import doctest
import sys

def trace_runner(reporter,arg3) :
import sys
tb = sys.exc_info()[2]
while tb.tb_next : tb = tb.tb_next
l = tb.tb_frame.f_locals
out, test, example, arg3 = l['out'], l['test'], l['example'],
l[arg3]
runner = doctest.DocTestRunner(verbose=True,
optionflags=doctest.NORMALIZE_WHITESPACE)
getattr(runner,reporter)(out, test, example, arg3)
for e in test.examples :
e.source = 'pdb.set_trace();' + e.source
exec 'import pdb' in test.globs
return runner

mod_names = []
for mod_name in sys.argv[1:] :
__import__(mod_name,globals(),locals())
mod_names.append(mod_name)
finder = doctest.DocTestFinder()
runner = doctest.DebugRunner(optionflags=doctest.NORMALIZE_ WHITESPACE)

try :

for mod_name in mod_names :
mod = sys.modules.get(mod_name)
for test in finder.find(mod, mod_name) :
runner.run(test)

except doctest.DocTestFailure :

runner = trace_runner('report_failure','got')
runner.run(test)

except doctest.UnexpectedException, err :

runner = trace_runner('report_unexpected_exception','exc_in fo')
import pdb
pdb.post_mortem(err.exc_info[2])
runner.run(test)

Apr 8 '07 #1
  • viewed: 885
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ashley Lloyd | last post: by
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
2 posts views Thread by p.lavarre@ieee.org | last post: by
12 posts views Thread by thomas.guest@gmail.com | last post: by
6 posts views Thread by Bzyczek | last post: by
4 posts views Thread by petr.jakes.tpc@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.