简体   繁体   English

暂停 PyTest 并等待用户输入

[英]Pause PyTest and wait for user input

How can I pause a running test and wait for user input?如何暂停正在运行的测试并等待用户输入?

I have an unconventional test (python 3, pytest) where I want to "pause" to ask the user (me) for a id code before continuing on with the test.我有一个非常规测试(python 3,pytest),在继续测试之前,我想“暂停”向用户(我)询问 id 代码。

I'm aware this is not standard, and would like to know how to solve the problem.我知道这不是标准的,并且想知道如何解决这个问题。

Example Test:示例测试:

    def test_oauth_flow(self):
        url_jwt = auth(id=client_id, scope=scope)
        id_code = webbrowser.open_new(url_jwt)
        # pause wait for user input.
        # use code to complete flow etc....
        auth = auth(code)
        assert auth is 1

Well, it will always depend on what are you testing but my recommendation is to run ContractTests against the external API and record the responses to latter have this data available for your functional tests.好吧,它始终取决于您要测试的内容,但我的建议是针对外部 API 运行 ContractTests 并记录对后者的响应,以便这些数据可用于您的功能测试。

To record the responses you can use vcr, its a Ruby tool: https://github.com/vcr/vcr , but there is also a python implementation (I haven't used it) https://github.com/kevin1024/vcrpy要记录响应,您可以使用 vcr,它是一个 Ruby 工具: https : //github.com/vcr/vcr ,但也有一个 python 实现(我没有使用过) https://github.com/kevin1024 /vcrpy

From here , it's possible but pytest's default capturing needs to be dialed back to sys :这里开始,这是可能的,但 pytest 的默认捕获需要拨回sys

$ pytest --capture=sys ./tests/

Then this test function will halt the tests with a user prompt:然后这个测试函数将通过用户提示停止测试:

def fd_input(prompt):
    with os.fdopen(os.dup(1), "w") as stdout:
        stdout.write("\n{}? ".format(prompt))

    with os.fdopen(os.dup(2), "r") as stdin:
        return stdin.readline()

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

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