[TIP] Testing optparse with Nosetests

Dan Wandschneider daniel.wandschneider at schrodinger.com
Wed Oct 31 10:59:44 PDT 2012


Tim-
Yeah, that seems... not ideal.  I'd strongly recommend against adding these
options to your parser.  It sounds like get_opts() is probably the name of
the theoretical function I was referring to as my_parsing_function().  I
truly would recommend that you give get_opts() an args parameter defaulting
to None.  Devs may whine about this, but it is good practice; they should
have done it in the first place.  This would also allow you to test
arguments that are not arguments to Nose.

If you definitely cannot change get_opts() (the recommended solution), you
could modify sys.argv.  I'd recommend a context manager:

    class ArgvHider(object):
        def __init__(self, *newargs):
            self._newargs = list(newargs)
        def __enter__(self):
            self._argv = sys.argv
            sys.argv = self._newargs
        def __exit__(self, exc_type, exc_value, traceback):
            sys.argv = self._argv

Use like:
    print sys.argv
    with ArgvHider('-arg1', 'arg1val'):
        #argv is overridden with my custom values!
        print sys.argv
    #now the original argv is back!
    print sys.argv

-Dan



On Tue, Oct 30, 2012 at 11:56 PM, Tim Aerdts <fragger123 at gmail.com> 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> 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> 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
>>>
>>>
>>
>> _______________________________________________
>> testing-in-python mailing list
>> testing-in-python at lists.idyll.org
>> http://lists.idyll.org/listinfo/testing-in-python
>>
>>
>
>
> --
> Kind regards,
> Tim Aerdts
> http://www.tuimz.nl
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20121031/eb8a0a63/attachment-0001.htm>


More information about the testing-in-python mailing list