我怎么做py.测试测试接受交互式输入?

2024-06-08 09:42:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用py.测试有点非传统的申请。基本上,我希望在测试中通过print()和input()进行用户交互(这是python3.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捕获时,这种方法是有效的,但是在py.测试文档不起作用,因为它们只影响stdout和stderr。在

在py.测试模块,没有命令行选项,理想情况下是每次测试?在

这个平台,就其价值而言,就是Windows,我不希望有这样的破坏,也不希望被stdin/out/任何由嵌套shell、不常见的shell等产生的东西包装起来


Tags: the用户pyinput硬件软件stdinstdout
2条回答

有一个完全符合要求的包裹。在

https://github.com/wooey/Wooey

Wooey is a simple web interface to run command line Python scripts. Think of it as an easy way to get your scripts up on the web

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

no command line options

使用pytest.ini文件option或envvariable,以避免每次都使用命令行选项。在

ideally per-test?

使用函数作用域fixture获取用户输入。示例代码:

# contents of conftest.py
import pytest
@pytest.fixute(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"

相关问题 更多 >