如何让py.test测试接受交互式输入?

ikr*_*ase 7 python pytest

我正在使用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
Run Code Online (Sandbox Code Playgroud)

这适用于使用pytest -s调用测试文件以防止stdin和stdout捕获,但with capsys.disabled()py.test文档中记录的means()不起作用,因为它们只影响stdout和stderr.

使用py.test模块中的代码,没有命令行选项,理想情况下每次测试,这是一个很好的方法吗?

该平台,它的价值,是Windows,我宁愿没有这个clobber或被包装stdin/out /由嵌套shell,罕见的shell等产生的任何东西破坏.

Sil*_*Guy 3

没有命令行选项

使用 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"
Run Code Online (Sandbox Code Playgroud)