473,403 Members | 2,183 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,403 software developers and data experts.

doctest, sys.stderr, SystemExit and unused return values

I have an optparse-like module and though I have a full unittest-style
suite of tests for it, I'd also like to be able to run doctest on the
documentation to make sure my examples all work. However, I see that
doctest (1) doesn't capture anything from sys.stderr, and (2) unlike the
normal interpreter, generates a traceback for SystemExit.

Here's what some of my tests look like and the doctest output I get::

========== in my documentation ==========
>>parser.print_help()
usage: PROG [-h] [-w W] [-x X [X ...]] [y] [z [z ...]]

optional arguments:
-h, --help show this help message and exit
-w W w help
-x X [X ...] x help

positional arguments:
y y help
z z help

========== the doctest output ==========
Failed example:
parser.print_help()
Expected:
usage: PROG [-h] [-w W] [-x X [X ...]] [y] [z [z ...]]
========== in my documentation ==========
>>parser.parse_args('xyz --b 42'.split())
usage: PROG xyz [-h] [--x XXX] [-y] z
PROG xyz: error: no such option: --b

========== the doctest output ==========
Failed example:
parser.parse_args('xyz --b 42'.split())
Exception raised:
Traceback (most recent call last):
...
SystemExit: 2

Just like with optparse, calling ``print_help()`` and ``parse_args()``
prints to sys.stderr, and ``parse_args()`` raises SystemExit when it
encounters an error.

Is there a way to get these tests to pass?
On a related note, my examples currently use a lot of ``_ =`` because,
like optparse, I quite frequently don't care about the return value of
calls to add arguments, e.g.::
>>parser = argparse.ArgumentParser(prog='PROG')
_ = parser.add_optional('--foo', help='foo help')
_ = parser.add_positional('bar', help='bar help')
values = parser.parse_args('--foo spam badger'.split())
values.foo
'spam'
>>values.bar
'badger'

Is there any way to avoid having to write the ``_ =`` without getting an
error like::

Failed example:
parser.add_optional('--foo', help='foo help')
Expected nothing
Got:
Optional('--foo', dest='foo', action=None, ...

I don't want to include the whole Optional repr() string because it's
long and it distracts from the point of the example.

STeVe

Jul 1 '06 #1
0 1239

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

Similar topics

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...
1
by: David MacKay | last post by:
Hello, I'm a python-list newbie. I've got a question about doctest; perhaps a bug report. I really like doctest, but sometimes doctest gives a failure when the output looks absolutely fine to me...
1
by: Runsun Pan | last post by:
I intend to use the doctect heavily. For this I am thinking of coding a class that comes with a built-in doctest functionality. I'd like to seek for input before I start. The idea is to have a...
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...
3
by: GinTon | last post by:
Is the same use _sys.stderr.write('error message'); sys.exit(1)_ than _sys.exit('error message')_ ? Note: help(sys.exit) If the status is omitted or None, it defaults to zero (i.e., success)....
0
by: Eric Mahurin | last post by:
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...
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...
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
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
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...
0
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,...
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...
0
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,...
0
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...

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.