简体   繁体   English

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

[英]How can I make py.test tests accept interactive input?

I'm using py.test for a somewhat unconventional application.我将 py.test 用于一个有点非常规的应用程序。 Basically, I want to have user interaction in a test via print() and input() (this is Python 3.5).基本上,我希望通过 print() 和 input()(这是 Python 3.5)在测试中进行用户交互。 The ultimate goal is to have semi-automated testing for hardware and multi-layered software which cannot be automatically tested even in principle.最终目标是对硬件和多层软件进行半自动化测试,即使在原则上也无法进行自动测试。 Some test cases will ask the testing technician to do something (to be confirmed by enter or press any key or similar on the console) or ask them to take a simple measurement or visually confirm something (to be entered on the console).一些测试用例会要求测试技术人员做某事(通过控制台上的输入或按​​任意键或类似方式进行确认)或要求他们进行简单的测量或视觉确认某事(在控制台上输入)。

Example of what I (naively) want to do:我(天真地)想做的示例:

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

This works with when the test file is called with pytest -s to prevent stdin and stdout capturing, but the documented means ( with capsys.disabled() ) in the py.test documentation don't work since they only affect stdout and stderr.这适用于使用 pytest -s 调用测试文件以防止 stdin 和 stdout 捕获,但with capsys.disabled()文档中记录的方法( with capsys.disabled() )不起作用,因为它们只影响 stdout 和 stderr。

What's a good way to make this work using code in the py.test module , no command line options, and ideally per-test?使用py.test 模块中的代码,没有命令行选项,最好是按测试进行这项工作的好方法是什么?

The platform, for what it's worth, is Windows and I'd prefer not to have this clobber or get clobbered by the wrapping of stdin/out/whatever that results from nested shells, uncommon shells, etc.就其价值而言,该平台是 Windows,我不希望有这种破坏或被标准输入/输出/嵌套外壳、不常见外壳等导致的任何结果的包装破坏。

no command line options没有命令行选项

Use pytest.ini option or envvariable to avoid using command line option every time.使用 pytest.ini 选项或 env变量避免每次都使用命令行选项。

ideally per-test?理想情况下每次测试?

Use function scoped fixture to take user input.使用函数作用域夹具来获取用户输入。 Example code:示例代码:

# 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"

There is a package that will exactly fit the bill.有一个包完全符合要求。

https://github.com/wooey/Wooey https://github.com/wooey/Wooey

Wooey is a simple web interface to run command line Python scripts. Wooey 是一个简单的 Web 界面,用于运行命令行 Python 脚本。 Think of it as an easy way to get your scripts up on the web将其视为在网络上发布脚本的简单方法

Demo site: https://wooey.herokuapp.com/演示站点: https : //wooey.herokuapp.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM