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

<br></div><div>I will replace the empty list though, thanks :)</div><div><br></div><div>-Tim</div><div><br><div class="gmail_quote">On Thu, Nov 1, 2012 at 6:48 PM, Dan Wandschneider <span dir="ltr">&lt;<a href="mailto:daniel.wandschneider@schrodinger.com" target="_blank">daniel.wandschneider@schrodinger.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You might try this:<div><div><br></div><div><div>def opts(args=None):</div><div class="im"><div>    &quot;&quot;&quot;</div>

<div>        Using optparser to parse any commandline options.</div><div>        @return: </div><div>    &quot;&quot;&quot;</div>

<div>    parser = optparse.OptionParser()</div><div><br></div><div>    parser.add_option(....</div><div>    </div></div><div>    options, args = parser.parse_args(args)</div><div>    return args</div><div><br></div><div>

Then in tests do:</div>

<div>opts([&#39;-testarg&#39;])</div><div><br></div></div><div>Also, you shouldn&#39;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: <a href="http://stackoverflow.com/q/1132941/1309332" target="_blank">http://stackoverflow.com/q/1132941/1309332</a>.</div>

<div><div class="h5">

<div><br></div><div>         <br><div class="gmail_quote">On Wed, Oct 31, 2012 at 11:57 PM, Tim Aerdts <span dir="ltr">&lt;<a href="mailto:fragger123@gmail.com" target="_blank">fragger123@gmail.com</a>&gt;</span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<div><br></div><div>I&#39;ve decided to go with Dan&#39;s initial solution. But a little different.</div><div>In utils.py</div>



<div><br></div><div>ARGS = []</div><div><br></div><div><div>def opts():</div><div>    &quot;&quot;&quot;</div>

<div>        Using optparser to parse any commandline options.</div><div>        @return </div><div>    &quot;&quot;&quot;</div><div>    parser = optparse.OptionParser()</div><div><br></div><div>    parser.add_option(....</div>





<div>    </div><div>    if len(ARGS):</div><div>        options, args = parser.parse_args(ARGS)</div><div>    else:</div><div>        options, args = parser.parse_args()</div><div>    return options</div><div><br></div><div>





Then in my tests I do:</div><div>from utils import ARGS</div><div>ARGS.append(&#39;-testarg&#39;)</div><div><br></div><div>For example. This might not be very pythonic I don&#39;t know and I might refactor it at a later stage but for now it works.</div>





<div><br></div><div>Thank you for all your tips and help.</div><div>Oh and I couldn&#39;t use argparse because I am stuck with 2.6 where argparse was introduced in 2.7</div><div><br></div><div>Cheers,</div><div><br></div>





<div>-Tim</div><div><div><br><div class="gmail_quote">On Wed, Oct 31, 2012 at 6:59 PM, Dan Wandschneider <span dir="ltr">&lt;<a href="mailto:daniel.wandschneider@schrodinger.com" target="_blank">daniel.wandschneider@schrodinger.com</a>&gt;</span> wrote:<br>





<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Tim-<div>Yeah, that seems... not ideal.  I&#39;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.</div>







<div><br></div><div>If you definitely cannot change get_opts() (the recommended solution), you could modify sys.argv.  I&#39;d recommend a context manager:</div><div><br></div><div><div>    class ArgvHider(object):</div>






<div>
        def __init__(self, *newargs):</div><div>            self._newargs = list(newargs)</div><div>        def __enter__(self):</div><div>            self._argv = sys.argv</div><div>            sys.argv = self._newargs</div>







<div>        def __exit__(self, exc_type, exc_value, traceback):</div><div>            sys.argv = self._argv</div></div><div><br></div><div>Use like:</div><div><div>    print sys.argv</div><div>    with ArgvHider(&#39;-arg1&#39;, &#39;arg1val&#39;):</div>







<div>        #argv is overridden with my custom values!</div><div>        print sys.argv</div><div>    #now the original argv is back!</div><div>    print sys.argv</div></div><span><font color="#888888"><div>

<br></div><div>-Dan</div></font></span><div><div><div><br></div>
<div>
<br></div><div><br><div class="gmail_quote">On Tue, Oct 30, 2012 at 11:56 PM, Tim Aerdts <span dir="ltr">&lt;<a href="mailto:fragger123@gmail.com" target="_blank">fragger123@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Dan,<div><br></div><div>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.</div>










<div><br></div><div>I&#39;ve more or less solved it by adding </div><div><br></div><div><div>    parser.add_option(&#39;--with-coverage&#39;)</div><div>    parser.add_option(&#39;--with-xunit&#39;)</div><div>    parser.add_option(&#39;--cover-xml&#39;)</div>










<div>    parser.add_option(&#39;--verbose&#39;)</div><div>    parser.add_option(&#39;--cover-package&#39;)</div><div>    parser.add_option(&#39;--nocapture&#39;)</div></div><div><br></div><div>To the parser, but I am sure this is not the way to go.</div>










<div><br></div><div>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</div>










<div><br></div><div>Cheers,</div><div><br></div><div>-Tim</div><div><div><div><br><div class="gmail_quote">On Tue, Oct 30, 2012 at 8:45 PM, Dan Wandschneider <span dir="ltr">&lt;<a href="mailto:daniel.wandschneider@schrodinger.com" target="_blank">daniel.wandschneider@schrodinger.com</a>&gt;</span> wrote:<br>










<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Tim-</div><div>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:</div>












<div>    argslist = [&#39;-h&#39;]</div><div>    with assertRaises(SystemExit):</div><div>        myparser.parse_args(argslist)</div><div><br></div><div>If your parser is wrapped in a function or method, use this:</div><div>












    def my_parsing_function(args=None):</div><div>        myparser.parse_args(args)</div><div><br></div><div>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)</div>












<div><br></div>-Dan W.<div><a href="http://www.schrodinger.com/" target="_blank">http://www.schrodinger.com/</a><br><br><div class="gmail_quote">On Tue, Oct 30, 2012 at 12:00 PM,  <span dir="ltr">&lt;<a href="mailto:testing-in-python-request@lists.idyll.org" target="_blank">testing-in-python-request@lists.idyll.org</a>&gt;</span> wrote:<br>












<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Send testing-in-python mailing list submissions to<br>
        <a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
        <a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
or, via email, send a message with subject or body &#39;help&#39; to<br>
        <a href="mailto:testing-in-python-request@lists.idyll.org" target="_blank">testing-in-python-request@lists.idyll.org</a><br>
<br>
You can reach the person managing the list at<br>
        <a href="mailto:testing-in-python-owner@lists.idyll.org" target="_blank">testing-in-python-owner@lists.idyll.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than &quot;Re: Contents of testing-in-python digest...&quot;<br>
<br>Today&#39;s Topics:<br>
<br>
   1. Testing optparse with Nosetests (Tim Aerdts)<br>
   2. Re: Testing optparse with Nosetests (Tim Aerdts)<br>
<br><br>---------- Forwarded message ----------<br>From: Tim Aerdts &lt;<a href="mailto:fragger123@gmail.com" target="_blank">fragger123@gmail.com</a>&gt;<br>To: <a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a><br>












Cc: <br>Date: Tue, 30 Oct 2012 09:42:49 +0100<br>Subject: [TIP] Testing optparse with Nosetests<br>Hello,<div><br></div><div>Posting this here because it seems more active then the Nosetests users list.</div><div>Anyway I am in the process of writing an application which makes use of Optparse. I&#39;m running the tests with Nosetests but I fear these two might be interfering?</div>














<div><br></div><div>When I run parser.parse_args() anywhere in my code Nosetests bugs out.</div><div><br></div><div><div>Usage: nosetests [options]</div><div>nosetests: error: no such option: --with-coverage</div><div><br>














</div><div>Removing the call to parse_args() and everything runs fine.</div><div><br></div><div>Cheers,</div><div><br></div>-- <br>Kind regards,<br>Tim Aerdts<br><a href="http://www.tuimz.nl" target="_blank">http://www.tuimz.nl</a><br>













</div>
<br><br>---------- Forwarded message ----------<br>From: Tim Aerdts &lt;<a href="mailto:fragger123@gmail.com" target="_blank">fragger123@gmail.com</a>&gt;<br>To: <a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a><br>












Cc: <br>Date: Tue, 30 Oct 2012 11:00:53 +0100<br>Subject: Re: [TIP] Testing optparse with Nosetests<br>Just a follow-up.<div><br></div><div>If I add the options that I use with nosetests (--with-coverage --verbose --cover-package=mypackage) to my own optparser it works as expected.</div>












<div><br></div><div>I don&#39;t understand this, and preferably I don&#39;t need those dependencies in my main application..</div>

<div><br><div class="gmail_quote">On Tue, Oct 30, 2012 at 9:42 AM, Tim Aerdts <span dir="ltr">&lt;<a href="mailto:fragger123@gmail.com" target="_blank">fragger123@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">














Hello,<div><br></div><div>Posting this here because it seems more active then the Nosetests users list.</div><div>Anyway I am in the process of writing an application which makes use of Optparse. I&#39;m running the tests with Nosetests but I fear these two might be interfering?</div>















<div><br></div><div>When I run parser.parse_args() anywhere in my code Nosetests bugs out.</div><div><br></div><div><div>Usage: nosetests [options]</div><div>nosetests: error: no such option: --with-coverage</div><div><br>















</div><div>Removing the call to parse_args() and everything runs fine.</div><div><br></div><div>Cheers,</div><span><font color="#888888"><span><font color="#888888"><div><br></div>-- <br>Kind regards,<br>Tim Aerdts<br>

<a href="http://www.tuimz.nl" target="_blank">http://www.tuimz.nl</a><br>




</font></span></font></span></div><span><font color="#888888">
</font></span></blockquote></div><span><font color="#888888"><br><br clear="all"><div><br></div>-- <br>Kind regards,<br>Tim Aerdts<br><a href="http://www.tuimz.nl" target="_blank">http://www.tuimz.nl</a><br>


</font></span></div><span><font color="#888888">
<br>_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br></font></span></blockquote></div><br></div>
<br>_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Kind regards,<br>Tim Aerdts<br><a href="http://www.tuimz.nl" target="_blank">http://www.tuimz.nl</a><br>
</div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Kind regards,<br>Tim Aerdts<br><a href="http://www.tuimz.nl" target="_blank">http://www.tuimz.nl</a><br>
</div></div></div>
</blockquote></div><br></div></div></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Kind regards,<br>Tim Aerdts<br><a href="http://www.tuimz.nl">http://www.tuimz.nl</a><br>
</div>