[Python-Dev] unittest argv
Hi, main() in unittest has an optional parameter called argv. If it is not present in the invocation, it defaults to None. Later in the function a check is made to see if argv is None and if so sets it to sys.argv. I think the default should be changed to sys.argv[1:] (i.e. the command line arguments minus the name of the python file being executed). The parseArgs() function then uses getopt to parse argv. It currently ignores the first item in the argv list, but this causes a problem when it is called from another python function and not from the command line. So using the current code if I call: python mytest.py -v then argv in parseArgs is ['mytest.py', '-v'] But, if I call: unittest.main(module=None, argv=['-v','mytest']) then argv in parseArgs is ['mytest'], as you can see the verbosity option is now gone and cannot be used. Here's a diff to show the code changes I have made: 744c744 < argv=None, testRunner=None, testLoader=defaultTestLoader): --- > argv=sys.argv[1:], testRunner=None, > testLoader=defaultTestLoader): 751,752d750 < if argv is None: < argv = sys.argv 757c755 < self.progName = os.path.basename(argv[0]) --- > #self.progName = os.path.basename(argv[0]) 769c767 < options, args = getopt.getopt(argv[1:], 'hHvq', --- > options, args = getopt.getopt(argv, 'hHvq', You may notice I have commented out the self.progName line. This variable is not used anywhere in the module so I guess it could be removed. To keep it then conditional check on argv would have to remain and be moved after the self.progName line. I hope this makes sense, and it's my first post so go easy on me ;) Thanks, -John K ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] unittest argv
On 5/1/06, Guido van Rossum <[EMAIL PROTECTED]> wrote: > Wouldn't this be an incompatible change? That would make it a no-no. > Providing a dummy argv[0] isn't so hard is it? It would be incompatible with existing code, but that code is already broken (IMO) by passing a dummy argv[0]. I don't think fixing it would affect much code, because normally people don't specify the '-q' or '-v' in code, it is almost exclusively used on the command line. The only reason I came across it was that I was modifying an ant task (py-test) so it could handle all of the named arguments that TestProgram.__init__ supports. If the list index code can't change, at a minimum the default value for argv should change from None to sys.argv. Are the tests for unittest.py? Thanks, -John K > > On 4/30/06, John Keyes <[EMAIL PROTECTED]> wrote: > > Hi, > > > > main() in unittest has an optional parameter called argv. If it is not > > present in the invocation, it defaults to None. Later in the function > > a check is made to see if argv is None and if so sets it to sys.argv. > > I think the default should be changed to sys.argv[1:] (i.e. the > > command line arguments minus the name of the python file > > being executed). > > > > The parseArgs() function then uses getopt to parse argv. It currently > > ignores the first item in the argv list, but this causes a problem when > > it is called from another python function and not from the command > > line. So using the current code if I call: > > > > python mytest.py -v > > > > then argv in parseArgs is ['mytest.py', '-v'] > > > > But, if I call: > > > > unittest.main(module=None, argv=['-v','mytest']) > > > > then argv in parseArgs is ['mytest'], as you can see the verbosity option is > > now gone and cannot be used. > > > > Here's a diff to show the code changes I have made: > > > > 744c744 > > < argv=None, testRunner=None, > > testLoader=defaultTestLoader): > > --- > > > argv=sys.argv[1:], testRunner=None, > > > testLoader=defaultTestLoader): > > 751,752d750 > > < if argv is None: > > < argv = sys.argv > > 757c755 > > < self.progName = os.path.basename(argv[0]) > > --- > > > #self.progName = os.path.basename(argv[0]) > > 769c767 > > < options, args = getopt.getopt(argv[1:], 'hHvq', > > --- > > > options, args = getopt.getopt(argv, 'hHvq', > > > > You may notice I have commented out the self.progName line. This variable > > is not used anywhere in the module so I guess it could be removed. To > > keep it then conditional check on argv would have to remain and be moved > > after > > the self.progName line. > > > > I hope this makes sense, and it's my first post so go easy on me ;) > > > > Thanks, > > -John K > > ___ > > Python-Dev mailing list > > [email protected] > > http://mail.python.org/mailman/listinfo/python-dev > > Unsubscribe: > > http://mail.python.org/mailman/options/python-dev/guido%40python.org > > > > > -- > --Guido van Rossum (home page: http://www.python.org/~guido/) > ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] unittest argv
On 5/1/06, Guido van Rossum <[EMAIL PROTECTED]> wrote: > On 5/1/06, John Keyes <[EMAIL PROTECTED]> wrote: > > On 5/1/06, Guido van Rossum <[EMAIL PROTECTED]> wrote: > > > Wouldn't this be an incompatible change? That would make it a no-no. > > > Providing a dummy argv[0] isn't so hard is it? > > > > It would be incompatible with existing code, but that code is > > already broken (IMO) by passing a dummy argv[0]. > > That's a new meaning of "broken", one that I haven't heard before. > It's broken because it follows the API?!?! Fair enough, a bad use of language on my part. > >I don't > > think fixing it would affect much code, because normally > > people don't specify the '-q' or '-v' in code, it is almost > > exclusively used on the command line. > > Famous last words. Probably ;) > > The only reason I came across it was that I was modifying > > an ant task (py-test) so it could handle all of the named > > arguments that TestProgram.__init__ supports. > > > > If the list index code can't change, at a minimum the default value > > for argv should change from None to sys.argv. > > No. Late binding of sys.argv is very important. There are plenty of > uses where sys.argv is dynamically modified. Can you explain this some more? If it all happens in the same function call so how can it be late binding? > > Are the tests for unittest.py? > > Assuming you meant "Are there tests", yes: test_unittest.py. But it needs > work. Ok thanks, -John K ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
