Search code examples
pythonsubprocesspopennosetestsos.system

ValueError using nosetest subprocess


I would like to have the same output that I got when I executed os.system:

os.system('nosetests TestStateMachine.py:FluidityTest.test_it_has_an_initial_state -v')
test_it_has_an_initial_state (TestStateMachine.FluidityTest) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Out[5]: 0

But when I'm executing:

x = subprocess.Popen(["nosetests",
"TestStateMachine.py:FluidityTest.test_it_has_an_initial_state -v"],
stdout=subprocess.PIPE)

I'm getting:

E
======================================================================
ERROR: Failure: ValueError (No such test FluidityTest.test_it_has_an_initial_state -v)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/nose/failure.py", line 41, in runTest
raise self.exc_class(self.exc_val)
ValueError: No such test FluidityTest.test_it_has_an_initial_state -v

----------------------------------------------------------------------
Ran 1 test in 0.034s

FAILED (errors=1)

Solution

  • You are using the wrong command line. Here's the correct one:

    x = subprocess.Popen(["nosetests",
        "TestStateMachine.py:FluidityTest.test_it_has_an_initial_state",
        "-v"],
        stdout=subprocess.PIPE)
    

    The list you pass to Popen already represents the parsed command line. By putting the test name and the -v in the same string it's like if you quoted them, like doing:

    $nosetests "TestStateMachine.py:FluidityTest.test_it_has_an_initial_state -v"
    

    On the command line, and the -v ends up in the name of the test (read carefully the error you got...)