473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
0 1104

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ashley Lloyd | last post by:
What do people generally use to debug their Python programs? I haven't seen anything out there that walks through the code, but perhaps I'm looking in the wrong places? TIA Ashley ...
14
by: Pierre Rouleau | last post by:
I have a problem writing self-testable modules using doctest when these modules have internationalized strings using gettext _('...'). - The main module of an application (say app.py) calls...
2
by: Alan G Isaac | last post by:
> python doctest.py -v Running doctest.__doc__ Trying: .remove(42) Expecting: Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: list.remove(x): x not in list ok...
2
by: Michele Simionato | last post by:
Some time ago I hacked a custom solution to run doctests on text files containing documentation. The solution involved this kind of game: tester=doctest.Tester(globs={},verbose=1)...
5
by: Michele Simionato | last post by:
I am getting a strange error with this script: $ cat doctest-threads.py """ >>> import time, threading >>> def example(): .... thread.out = .... while thread.running: .... ...
2
by: p.lavarre | last post by:
From: http://docs.python.org/lib/doctest-soapbox.html ... Can I somehow tell doctest that it's time to quit? I ask because not all doctest examples are created equal. Some failures are...
12
by: thomas.guest | last post by:
I'm not making progress with the following and would appreciate any help. Here's an interpreted Python session. .... Traceback (most recent call last): File "<stdin>", line 1, in <module>...
6
by: Bzyczek | last post by:
Hello, I have problems with running doctests if I use czech national characters in UTF-8 encoding. I have Python script, which begin with encoding definition: # -*- coding: utf-8 -*- I...
4
by: petr.jakes.tpc | last post by:
Hi, inspired by the article written by Tarek Ziade in the February 07 issue of the "Linux +" magazine I am experimenting with the doctest module. I have two files, "displeje_pokus.py" and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.