如何添加与pytest的stdin交互 [英] How to add stdin interaction with pytest

查看:185
本文介绍了如何添加与pytest的stdin交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个系统编写集成测试,该系统可以通过Web服务调用使大多数测试自动化,但是由于无法更改的遗留性,我需要手动测试人员完成一些步骤.

I am writing integration tests for a system where I can automate most of the test via web service calls, but due to legacy-ness that I cannot change, I need a few steps to be done by manual testers.

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

I wanted to use pytest and create a fixture that essentially pauses test execution and prompts the console for input (e.g. "Do XYZ in system; type 'done' when done") and continues with the remainder of the test.

我承认我还没有对此进行大量黑客攻击,但是我从pytest文档中看到:

I admittedly haven't done a ton of hacking on this yet, but I see from pytest docs that:

... stdin设置为"null"对象,如果尝试执行以下操作将失败 从中读取内容,因为很少需要等待交互 在运行自动化测试时输入.

... stdin is set to a "null" object which will fail on attempts to read from it because it is rarely desired to wait for interactive input when running automated tests.

除了我而言,我真的很想等待,除此之外,我的东西似乎是pytest的绝佳用例.

Except, in my case, I really do want to wait, and other than this, my stuff looks to be a great use case for pytest.

仍在互连网上搜索提示,但是如果有人已经超过了这个障碍,我很想知道.

Still searching the interwebs for hints, but if someone has gotten past this roadblock already I'd love to know.

推荐答案

从版本3开始,您可以

As of version 3, you can temporarily disable the capture:

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 ========================================

这篇关于如何添加与pytest的stdin交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆