python - click.testing.CliRunner 和处理 SIGINT/SIGTERM 信号
问题描述
我想添加一些关于我的 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 测试(调用可执行文件而不是 CLIrunner),我认为这样做没有问题,但我想尝试实现这一点(至少能够发送 os.kill)
有办法吗?
解决方案
因此,如果您想测试您的点击驱动应用程序以处理不同的信号,您可以执行下一个过程。
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']
推荐阅读
- c++ - C ++如何检查2个相似文件中没有哪些单词
- jquery - horizontal scroll-able table
- javascript - 可访问性错误:控件和媒体元素应该有标签
- php - 图片和产品信息未显示在页面上
- fiware - ckanext-ngsiview 和 right_time_context 插件 CKAN 有什么区别?
- javascript - 更新角度服务中的变量
- wordpress - 需要一个可在整个 WordPress 中访问的自定义函数
- html - 使 BootStrap 居中
- python - 定义后更改 SQLAlchemy 主键
- robotframework - 获取 [Errno 10061] 无法建立连接,因为目标机器主动拒绝它> 执行 RF 代码时出错