Nose test script with command line arguments
Question: I would like to be able to run a nose test script which accepts command line
arguments. For example, something along the lines:

test.py

    
    
    import nose, sys
    
    def test():
        # do something with the command line arguments
        print sys.argv
    
    if __name__ == '__main__':
        nose.runmodule()
    

However, whenever I run this with a command line argument, I get an error:

    
    
    $ python test.py arg
    E
    ======================================================================
    ERROR: Failure: ImportError (No module named arg)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/loader.py", line 368, in loadTestsFromName
        module = resolve_name(addr.module)
      File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/util.py", line 334, in resolve_name
        module = __import__('.'.join(parts_copy))
    ImportError: No module named arg
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    FAILED (errors=1)
    

Apparently, nose tries to do something with the arguments passed in sys.argv.
Is there a way to make nose ignore those arguments?


Answer: Alright, I hate "why would you want to do that?" answers just as much as
anyone, but I'm going to have to make one here. I hope you don't mind.

I'd argue that doing whatever you're wanting to do isn't within the scope of
the framework nose. Nose is intended for _automated_ tests. If you have to
pass in command-line arguments for the test to pass, then it isn't automated.
Now, what you _can_ do is something like this:

    
    
    import sys
    
    class test_something(object):
        def setUp(self):
            sys.argv[1] = 'arg'
            del sys.argv[2] # remember that -s is in sys.argv[2], see below
        def test_method(self):
            print sys.argv
    

If you run that, you get this output:

    
    
    [~] nosetests test_something.py -s
    ['/usr/local/bin/nosetests', 'arg']
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    OK
    

(Remember to pass in the -s flag if you want to see what goes on stdout)

However, I'd probably still recommend against that, as it's generally a bad
idea to mess with global state in automated tests if you can avoid it. What I
would likely do is adapt whatever code I'm wanting to test to take an `argv`
list. Then, you can pass in whatever you want during testing and pass in
`sys.argv` in production.

**UPDATE** :

> The reason why I need to do it is because I am testing multiple
> implementations of the same library. To test those implementations are
> correct I use a single nose script, that accepts as a command line argument
> the library that it should import for testing.

It sounds like you may want to try your hand at writing a nose plugin. It's
pretty easy to do. [Here are the latest
docs.](http://nose.readthedocs.org/en/latest/plugins/writing.html)

