使用nosetest子进程的ValueError

时间:2014-01-14 21:18:11

标签: python subprocess popen nosetests os.system

我希望在执行 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

但是当我执行时:

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

我得到了:

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)

1 个答案:

答案 0 :(得分:2)

您使用的命令行错误。这是正确的:

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

您传递给Popen的列表已经代表已解析的命令行。通过将测试名称和-v放在同一个字符串中,就像你引用它们一样,如:

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

在命令行上,-v以测试名称结束(仔细阅读你得到的错误......)