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

时间:2016-12-17 00:34:01

标签: 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

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

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

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

2 个答案:

答案 0 :(得分:0)

  

没有命令行选项

使用pytest.ini option或env variable避免每次都使用命令行选项。

  

理想的是每次测试?

使用功能范围的夹具来接受用户输入。示例代码:

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

答案 1 :(得分:-6)

有一个完全适合账单的套餐。

https://github.com/wooey/Wooey

  

Wooey是一个运行命令行Python脚本的简单Web界面。   您可以将其视为在网络上创建脚本的简便方法

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

相关问题