nose测试在raw_input处冻结

3 投票
1 回答
1287 浏览
提问于 2025-04-17 13:00

我有一个鼻子测试,它会导入一个文件,然后运行一个包含 raw_inputs 的类。每次我在命令行输入 nosetests 时,提示符就会停在那里,不继续往下走——我必须按下键盘的中断键才能看到发生了什么。结果发现,鼻子测试正在运行我的文件,直到遇到第一个 raw_input(还有很多个),到那时它就停住了,无法继续。

有没有办法绕过这个问题?谢谢!

1 个回答

4

如果可以的话,重写这个文件,让它在被导入时不要调用raw_input()。

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

否则,如果你能提前知道什么样的输入是可以接受的,你可以从一个文件中读取标准输入。假设input.txt文件里有“Pass”:

nosetests test_input.py < input.txt

这里test_input.py的内容是:

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

或者你可以把可以接受的输入通过管道传给nosetests:

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)

撰写回答