单击.testing.CliRunner以及处理SIGINT/SIGTERM信号

2024-05-19 00:23:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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测试(调用executable而不是CLIrunner),我认为这样做没有问题,但是我想尝试实现这个(至少能够发送杀死)你知道吗

有办法吗?你知道吗


Tags: no应用程序cliserver信号addressargsresult
1条回答
网友
1楼 · 发布于 2024-05-19 00:23:44

所以,如果您想测试您的点击供电的应用程序处理不同的信号,您可以做下一个过程。你知道吗

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']

相关问题 更多 >

    热门问题