如何限制单元测试的最大运行时间? [英] How can I limit the maximum running time for a unit test?

查看:55
本文介绍了如何限制单元测试的最大运行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在运行一些单元测试,这些测试可能需要很长时间才能失败或无限期运行.在成功的测试运行中,它们总是会在一定时间内完成.

I am currently running some unit tests that might either take a long time before failing or run indefinitely. In a successful test run they will always complete within a certain amount of time.

是否可以创建一个 pytest 单元测试,如果它没有在一定时间内完成就会失败?

Is it possible to create a pytest unit test that will fail if it does not complete within a certain amount of time?

推荐答案

您可以安装 pytest-timeout 插件,然后将您的测试函数标记为以秒为单位的超时时间.

you can install the pytest-timeout plugin and then mark your test functions with a timeout in seconds.

@pytest.mark.timeout(300)
def test_foo():
   pass

https://pypi.python.org/pypi查看插件下载和使用说明/pytest-超时

这篇关于如何限制单元测试的最大运行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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