【问题标题】:click.testing.CliRunner and handling SIGINT/SIGTERM signalsclick.testing.CliRunner 和处理 SIGINT/SIGTERM 信号
【发布时间】:2019-12-11 21:31:28
【问题描述】:

我想添加一些关于我的 cli 应用程序如何处理不同信号的测试(SIGTERM 等)。我在 pytest 旁边使用原生测试解决方案 click.testing.CliRunner

测试看起来非常标准和简单

def test_breaking_process(server, runner):

    address = server.router({'^/$': Page("").exists().slow()})

    runner = CliRunner(mix_stderr=True)
    args = [address, '--no-colors', '--no-progress']
    result = runner.invoke(main, args)
    assert result.exit_code == 0

我在这里卡住了,我怎么能发送SIGTERM 来处理runner.invoke?如果我使用 e2e 测试(调用可执行文件而不是 CLIrunner),我认为这样做没有问题,但我想尝试实现这一点(至少能够发送 os.kill)

有什么办法吗?

【问题讨论】:

标签: python pytest python-click


【解决方案1】:

所以,如果你想测试你的点击驱动应用程序处理不同的信号,你可以做下一个过程。

def test_breaking_process(server, runner):

    from multiprocessing import Queue, Process
    from threading import Timer
    from time import sleep
    from os import kill, getpid
    from signal import SIGINT

    url = server.router({'^/$': Page("").slow().exists()})
    args = [url, '--no-colors', '--no-progress']

    q = Queue()

    # Running out app in SubProcess and after a while using signal sending 
    # SIGINT, results passed back via channel/queue  
    def background():
        Timer(0.2, lambda: kill(getpid(), SIGINT)).start()
        result = runner.invoke(main, args)
        q.put(('exit_code', result.exit_code))
        q.put(('output', result.output))

    p = Process(target=background)
    p.start()

    results = {}

    while p.is_alive():
        sleep(0.1)
    else:
        while not q.empty():
            key, value = q.get()
            results[key] = value

    assert results['exit_code'] == 0
    assert "Results can be inconsistent, as execution was terminated" in results['output']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-09
    • 2020-07-03
    • 2016-01-20
    • 2020-05-11
    • 2013-12-21
    • 1970-01-01
    • 2023-04-02
    • 2020-10-19
    相关资源
    最近更新 更多