鼻子测试冻结在 raw_input

发布于 2025-01-05 12:24:47 字数 174 浏览 0 评论 0原文

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

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

I have a nose test that imports a file which runs a class with raw_inputs. Whenever I type nosetests in the command line, the prompt simply pauses and doesn't continue - I have to keyboard interrupt to see what happens, and it turns out the nose test is running my file up to the first raw_input (one of many), at which point it simply pauses and cannot continue.

Any way to bypass this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2025-01-12 12:24:47

如果可能,请重写该文件,以便在导入时不会调用 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"

或者您可以将可接受的输入传输到 Nostests 中:

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)

If possible, rewrite the file so it won't call raw_input() when imported.

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

Otherwise, if you can figure out in advance what would be acceptable input, you can take standard input from a file. Assuming input.txt contains "Pass":

nosetests test_input.py < input.txt

where test_input.py is:

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

Or you can pipe acceptable input into 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文