[TIP] Testing optparse with Nosetests

Ned Batchelder ned at nedbatchelder.com
Wed Oct 31 04:15:20 PDT 2012


The problem is that parse_args() is implicitly accessing shared global 
state, sys.argv.  You need to refactor your code so that you can control 
what argument list it accesses, as Dan described. You'll need to 
refactor you code down to that call, and pass the arguments to it 
explicitly.

--Ned.

On 10/31/2012 2:56 AM, Tim Aerdts wrote:
> Hello Dan,
>
> Thanks for your reply! However I can not use a custom argslist. I am 
> testing some methods and functions that call a function get_opts() 
> which parses the options. So I do not do any parse_args() calls in my 
> tests or anywhere near it.
>
> I've more or less solved it by adding
>
>     parser.add_option('--with-coverage')
>     parser.add_option('--with-xunit')
>     parser.add_option('--cover-xml')
>     parser.add_option('--verbose')
>     parser.add_option('--cover-package')
>     parser.add_option('--nocapture')
>
> To the parser, but I am sure this is not the way to go.
>
> Also my setup of optparse might not be that ideal. Everytime I need to 
> get something from the arguments I make a call to get_opts() which 
> returns the options from parse_args() so if I need something called 
> myval I always do get_opts().myval
>
> Cheers,
>
> -Tim
>
> On Tue, Oct 30, 2012 at 8:45 PM, Dan Wandschneider 
> <daniel.wandschneider at schrodinger.com 
> <mailto:daniel.wandschneider at schrodinger.com>> wrote:
>
>     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
>     <mailto:testing-in-python-request at lists.idyll.org>> wrote:
>
>         Send testing-in-python mailing list submissions to
>         testing-in-python at lists.idyll.org
>         <mailto: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
>         <mailto: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
>         <mailto: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
>         <mailto:fragger123 at gmail.com>>
>         To: testing-in-python at lists.idyll.org
>         <mailto: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
>         <mailto:fragger123 at gmail.com>>
>         To: testing-in-python at lists.idyll.org
>         <mailto: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 <mailto: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
>         <mailto:testing-in-python at lists.idyll.org>
>         http://lists.idyll.org/listinfo/testing-in-python
>
>
>
>     _______________________________________________
>     testing-in-python mailing list
>     testing-in-python at lists.idyll.org
>     <mailto:testing-in-python at lists.idyll.org>
>     http://lists.idyll.org/listinfo/testing-in-python
>
>
>
>
> -- 
> 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/20121031/0a5bbc05/attachment.html>


More information about the testing-in-python mailing list