【问题标题】:How to add stdin interaction with pytest如何添加与 pytest 的 stdin 交互
【发布时间】:2017-04-23 19:01:27
【问题描述】:

我正在为一个可以通过 Web 服务调用自动化大部分测试的系统编写集成测试,但由于我无法更改的遗留问题,我需要手动测试人员完成几个步骤。

我想使用 pytest 并创建一个夹具,该夹具实质上会暂停测试执行并提示控制台输入(例如“在系统中执行 XYZ;完成后键入‘完成’”)并继续进行其余的测试。

诚然,我还没有对此进行大量破解,但我从 pytest 文档中看到:

... stdin 设置为“null”对象,该对象将在尝试 从中读取,因为很少需要等待交互 运行自动化测试时的输入。

除了,就我而言,我真的很想等待,除此之外,我的东西看起来是 pytest 的一个很好的用例。

仍在互联网上搜索提示,但如果有人已经克服了这个障碍,我很想知道。

【问题讨论】:

  • 注意:在发布此内容之前我不尝试 sys.stdin 感觉很糟糕,这将是我重新连接后做的第一件事(只是为了确认我的担忧是成立的)

标签: python testing integration-testing pytest


【解决方案1】:

从第 3 版开始,您可以temporarily disable 捕获:

def test_input(capsys):
    with capsys.disabled():
        input("hit enter to continue: ")
    print("this line is invisible as normal")

给予

(py36) dsm@notebook:~/coding$ py.test -v stdin.py 
========================================== test session starts ===========================================
platform linux -- Python 3.6.0, pytest-3.0.7, py-1.4.32, pluggy-0.4.0 -- /home/dsm/sys/miniconda3/envs/py36/bin/python
cachedir: .cache
rootdir: /home/dsm/coding, inifile:
plugins: cov-2.3.1
collected 1 items 

stdin.py::test_input hit enter to continue: [here I hit enter]
PASSED

======================================= 1 passed in 23.11 seconds ========================================

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-18
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多