Nose test script with command line arguments

后端 未结 5 1373
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 06:54

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
         


        
5条回答
  •  爱一瞬间的悲伤
    2021-02-04 07:50

    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.

提交回复
热议问题