3

我有一个鼻子测试,它导入一个运行带有 raw_inputs 的类的文件。每当我在命令行中键入nosetests 时,提示都会暂停并且不会继续-我必须通过键盘中断来查看发生了什么,事实证明nose test 正在将我的文件运行到第一个raw_input(许多之一) ,此时它只是暂停并且无法继续。

有什么办法绕过这个吗?谢谢!

4

1 回答 1

4

如果可能,请重写文件,以便在导入时不会调用 raw_input()。

# imported file
if __name__ == "__main__":
    raw_input()

否则,如果您可以提前弄清楚什么是可接受的输入,您可以从文件中获取标准输入。假设 input.txt 包含“通过”:

nosetests test_input.py < input.txt

其中 test_input.py 是:

# test file
def test_input():
    s = raw_input()
    assert s.strip() == "Pass"

或者您可以将可接受的输入输入到鼻子测试中:

c:\>echo Pass | nosetests test_input.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK

c:\>echo Fail | nosetests test_input.py
F
======================================================================
FAIL: cgp.test.test_input.test_input
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\nose\case.py", line 187, in runTest
    self.test(*self.arg)
  File "c:\test_input.py", line 3, in test_input
    assert s.strip() == "Pass"
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=1)
于 2012-02-15T12:49:43.530 回答