[TIP] Testing optparse with Nosetests

Dan Wandschneider daniel.wandschneider at schrodinger.com
Tue Oct 30 12:45:19 PDT 2012


Tim-
optparse.parse_args() parses all arguments given on the command line, so it
is probably balking on the commands that you gave to Nose.  In tests, you
should create the specific argument list that you want to test, and then
run parser.parse_args(arglist).  For example:
    argslist = ['-h']
    with assertRaises(SystemExit):
        myparser.parse_args(argslist)

If your parser is wrapped in a function or method, use this:
    def my_parsing_function(args=None):
        myparser.parse_args(args)

If args == None, OptParse will use sys.argv, so your production code is
safe.  (Also, if you are writing new code, I believe that ArgParse is
preferred to OptParse)

-Dan W.
http://www.schrodinger.com/

On Tue, Oct 30, 2012 at 12:00 PM, <testing-in-python-request at lists.idyll.org
> wrote:

> Send testing-in-python mailing list submissions to
>         testing-in-python at lists.idyll.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://lists.idyll.org/listinfo/testing-in-python
> or, via email, send a message with subject or body 'help' to
>         testing-in-python-request at lists.idyll.org
>
> You can reach the person managing the list at
>         testing-in-python-owner at lists.idyll.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of testing-in-python digest..."
>
> Today's Topics:
>
>    1. Testing optparse with Nosetests (Tim Aerdts)
>    2. Re: Testing optparse with Nosetests (Tim Aerdts)
>
>
> ---------- Forwarded message ----------
> From: Tim Aerdts <fragger123 at gmail.com>
> To: testing-in-python at lists.idyll.org
> Cc:
> Date: Tue, 30 Oct 2012 09:42:49 +0100
> Subject: [TIP] Testing optparse with Nosetests
> Hello,
>
> Posting this here because it seems more active then the Nosetests users
> list.
> Anyway I am in the process of writing an application which makes use of
> Optparse. I'm running the tests with Nosetests but I fear these two might
> be interfering?
>
> When I run parser.parse_args() anywhere in my code Nosetests bugs out.
>
> Usage: nosetests [options]
> nosetests: error: no such option: --with-coverage
>
> Removing the call to parse_args() and everything runs fine.
>
> Cheers,
>
> --
> Kind regards,
> Tim Aerdts
> http://www.tuimz.nl
>
>
> ---------- Forwarded message ----------
> From: Tim Aerdts <fragger123 at gmail.com>
> To: testing-in-python at lists.idyll.org
> Cc:
> Date: Tue, 30 Oct 2012 11:00:53 +0100
> Subject: Re: [TIP] Testing optparse with Nosetests
> Just a follow-up.
>
> If I add the options that I use with nosetests (--with-coverage --verbose
> --cover-package=mypackage) to my own optparser it works as expected.
>
> I don't understand this, and preferably I don't need those dependencies in
> my main application..
>
> On Tue, Oct 30, 2012 at 9:42 AM, Tim Aerdts <fragger123 at gmail.com> wrote:
>
>> Hello,
>>
>> Posting this here because it seems more active then the Nosetests users
>> list.
>> Anyway I am in the process of writing an application which makes use of
>> Optparse. I'm running the tests with Nosetests but I fear these two might
>> be interfering?
>>
>> When I run parser.parse_args() anywhere in my code Nosetests bugs out.
>>
>> Usage: nosetests [options]
>> nosetests: error: no such option: --with-coverage
>>
>> Removing the call to parse_args() and everything runs fine.
>>
>> Cheers,
>>
>> --
>> Kind regards,
>> Tim Aerdts
>> http://www.tuimz.nl
>>
>
>
>
> --
> Kind regards,
> Tim Aerdts
> http://www.tuimz.nl
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20121030/d24dbc70/attachment.htm>


More information about the testing-in-python mailing list