[Tutor] Unit testing command-line options from argparse or optparse

Serdar Tumgoren zstumgoren at gmail.com
Tue May 18 22:49:15 CEST 2010


Hello all,

Does anyone have advice for writing unit tests against variables set by
command-line options?

I have a program I'd like to run in either "debug" or "live" mode, with
various settings associated with each. Below is some pseudo-code that shows
what I'd like to do:

<<snipped argparse import and options setup >>
mode = p.parse_args() #always set to either --debug or --live

if mode.live:
    recipients = ['jsmith at email.com', 'janedoe at email.com']
    # set logging to a file
elif mode.debug:
    recipients = ['admin at admin.com']
    # log to stdout

The "live" and "debug" attributes are set by command-line flags passed to
the argparse module. What I'd like to do is write tests that check whether
various settings (recipients, logging, etc.) are configured properly based
on the command-line options.

But if "mode" is not set until runtime, I clearly can't import it into my
suite of unit tests, right? Is there some standard testing approach to this
problem (perhaps mocking?) that you all can recommend?

I'd greatly appreciate it.
Serdar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100518/1a0bd6ed/attachment.html>


More information about the Tutor mailing list