2012-02-14 51 views
3

我有一个鼻子测试,导入运行一个类与raw_inputs的文件。每当我在命令行中键入nosetests时,提示只是暂停并且不会继续 - 我必须键盘中断才能看到会发生什么,而事实证明鼻子测试正在运行我的文件直到第一个raw_input(其中之一) ,在这一点上,它只是暂停,不能继续。鼻子测试冻结在raw_input

任何方式绕过这个?谢谢!

回答

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" 

或者可以通过管道接受输入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)