[英]How can I make py.test tests accept interactive input?
我将 py.test 用于一个有点非常规的应用程序。 基本上,我希望通过 print() 和 input()(这是 Python 3.5)在测试中进行用户交互。 最终目标是对硬件和多层软件进行半自动化测试,即使在原则上也无法进行自动测试。 一些测试用例会要求测试技术人员做某事(通过控制台上的输入或按任意键或类似方式进行确认)或要求他们进行简单的测量或视觉确认某事(在控制台上输入)。
我(天真地)想做的示例:
def test_thingie():
thingie_init('red')
print('Testing the thingie.')
# Ask the testing technician to enter info, or confirm that he has set things up physically
x = int(input('Technician: How many *RED* widgets are on the thingie? Enter integer:'))
assert x == the_correct_number
这适用于使用 pytest -s 调用测试文件以防止 stdin 和 stdout 捕获,但with capsys.disabled()
文档中记录的方法( with capsys.disabled()
)不起作用,因为它们只影响 stdout 和 stderr。
使用py.test 模块中的代码,没有命令行选项,最好是按测试进行这项工作的好方法是什么?
就其价值而言,该平台是 Windows,我不希望有这种破坏或被标准输入/输出/嵌套外壳、不常见外壳等导致的任何结果的包装破坏。
没有命令行选项
使用 pytest.ini 选项或 env变量避免每次都使用命令行选项。
理想情况下每次测试?
使用函数作用域夹具来获取用户输入。 示例代码:
# contents of conftest.py
import pytest
@pytest.fixture(scope='function')
def take_input(request):
val = input(request.param)
return val
#Content of test_input.py
import pytest
@pytest.mark.parametrize('prompt',('Enter value here:'), indirect=True)
def test_input(take_input):
assert take_input == "expected string"
有一个包完全符合要求。
https://github.com/wooey/Wooey
Wooey 是一个简单的 Web 界面,用于运行命令行 Python 脚本。 将其视为在网络上发布脚本的简单方法
演示站点: https : //wooey.herokuapp.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.