473,722 Members | 2,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(re porter,arg3) :
import sys
tb = sys.exc_info()[2]
while tb.tb_next : tb = tb.tb_next
l = tb.tb_frame.f_l ocals
out, test, example, arg3 = l['out'], l['test'], l['example'],
l[arg3]
runner = doctest.DocTest Runner(verbose= True,
optionflags=doc test.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.appen d(mod_name)
finder = doctest.DocTest Finder()
runner = doctest.DebugRu nner(optionflag s=doctest.NORMA LIZE_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.DocTest Failure :

runner = trace_runner('r eport_failure', 'got')
runner.run(test )

except doctest.Unexpec tedException, err :

runner = trace_runner('r eport_unexpecte d_exception','e xc_info')
import pdb
pdb.post_mortem (err.exc_info[2])
runner.run(test )

Apr 8 '07 #1
0 1124

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

Similar topics

6
1630
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 _________________________________________________________________ Tired of 56k? Get a FREE BT Broadband connection
14
2634
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 gettext.install() to install the special _ function inside Python builtin. Other modules, taken from a general purpose collection of Python modules, also support internationalisation and doctest testing. For example:
2
1414
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 Trying: x = 12 Expecting: nothing
2
1676
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) tester.runstring(mytest) It worked fine, but now with Python 2.4.a3 I get DeprecationWarning: class Tester is deprecated; use class
5
1803
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: .... time.sleep(.01) .... thread.out.append(".")
2
2347
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 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
12
2116
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> NameError: name 'f' is not defined
6
2486
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 have this function with doctest:
4
1482
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 "displeje_pokus.txt" (you can see the simplified contents of the files bellow). When I run "python displeje_pokus.py" I am getting an error (see
0
8738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9381
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9151
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6680
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2146
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.