[Python-Dev] unittest argv

John Keyes john at integralsource.com
Mon May 1 03:59:32 CEST 2006


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:

<                  argv=None, testRunner=None, testLoader=defaultTestLoader):
>                  argv=sys.argv[1:], testRunner=None, testLoader=defaultTestLoader):
<         if argv is None:
<             argv = sys.argv
<         self.progName = os.path.basename(argv[0])
> #        self.progName = os.path.basename(argv[0])
<             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 ;)

-John K

More information about the Python-Dev mailing list