如何限制单元测试的最长运行时间?

时间:2022-03-11 07:15:22

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.

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

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

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

1 个解决方案

#1


36  

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

你可以安装pytest-timeout插件,然后以秒为单位标记你的测试函数。

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

Look at the plugin download and usage instructions at https://pypi.python.org/pypi/pytest-timeout

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

#1


36  

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

你可以安装pytest-timeout插件,然后以秒为单位标记你的测试函数。

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

Look at the plugin download and usage instructions at https://pypi.python.org/pypi/pytest-timeout

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