Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nose test script with command line arguments

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?

like image 879
D R Avatar asked Nov 02 '09 10:11

D R


People also ask

Which command is used to run nose tests?

Basic Usage nose can be integrated with DocTest by using with-doctest option in athe bove command line. The result will be true if the test run is successful, or false if it fails or raises an uncaught exception. nose supports fixtures (setup and teardown methods) at the package, module, class, and test level.

What is nose test Python?

Nose is a popular test automation framework in Python that extends unittest to make testing easier. The other advantages of using the Nose framework are the enablement of auto discovery of test cases and documentation collection.

What is Django nose?

django-nose provides all the goodness of nose in your Django tests, like: Testing just your apps by default, not all the standard ones that happen to be in INSTALLED_APPS. Running the tests in one or more specific modules (or apps, or classes, or folders, or just running a specific test)


1 Answers

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.

like image 54
Jason Baker Avatar answered Oct 09 '22 09:10

Jason Baker