在raw_input上进行鼻子测试冻结

时间:2012-02-14 11:20:25

标签: python nose nosetests raw-input

我有一个鼻子测试导入一个运行带有raw_inputs的类的文件。每当我在命令行输入nosetests时,提示只是暂停并且不会继续 - 我必须键盘中断才能看到发生了什么,并且结果是测试运行我的文件直到第一个raw_input(多个中的一个) ,此时它只是暂停而无法继续。

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

1 个答案:

答案 0 :(得分: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)