[TIP] Testing optparse with Nosetests

Tim Aerdts fragger123 at gmail.com
Fri Nov 2 02:32:41 PDT 2012


The thing is. I do not actually call opts() in any of my tests. I call it
in the functions which I am testing.
I don't think it makes sense to add parameters just for testing. So I will
keep it as it is.

I will replace the empty list though, thanks :)

-Tim

On Thu, Nov 1, 2012 at 6:48 PM, Dan Wandschneider <
daniel.wandschneider at schrodinger.com> wrote:

> You might try this:
>
> def opts(args=None):
>     """
>         Using optparser to parse any commandline options.
>         @return:
>     """
>     parser = optparse.OptionParser()
>
>     parser.add_option(....
>
>     options, args = parser.parse_args(args)
>     return args
>
> Then in tests do:
> opts(['-testarg'])
>
> Also, you shouldn't use an empty list as the default value for the module
> variable ARGS.  Instead, use None or possibly an empty tuple.  See this
> StackOverflow article for an explanation:
> http://stackoverflow.com/q/1132941/1309332.
>
>
> On Wed, Oct 31, 2012 at 11:57 PM, Tim Aerdts <fragger123 at gmail.com> wrote:
>
>> Hello,
>>
>> I've decided to go with Dan's initial solution. But a little different.
>> In utils.py
>>
>> ARGS = []
>>
>> def opts():
>>     """
>>         Using optparser to parse any commandline options.
>>         @return
>>     """
>>     parser = optparse.OptionParser()
>>
>>     parser.add_option(....
>>
>>     if len(ARGS):
>>         options, args = parser.parse_args(ARGS)
>>     else:
>>         options, args = parser.parse_args()
>>     return options
>>
>> Then in my tests I do:
>> from utils import ARGS
>> ARGS.append('-testarg')
>>
>> For example. This might not be very pythonic I don't know and I might
>> refactor it at a later stage but for now it works.
>>
>> Thank you for all your tips and help.
>> Oh and I couldn't use argparse because I am stuck with 2.6 where argparse
>> was introduced in 2.7
>>
>> Cheers,
>>
>> -Tim
>>
>> On Wed, Oct 31, 2012 at 6:59 PM, Dan Wandschneider <
>> daniel.wandschneider at schrodinger.com> wrote:
>>
>>> 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
>>>>
>>>
>>>
>>
>>
>> --
>> Kind regards,
>> Tim Aerdts
>> http://www.tuimz.nl
>>
>
>


-- 
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/20121102/37b031de/attachment.html>


More information about the testing-in-python mailing list