一. pip install pytest-timeout 常用
pip install pytest-timeouts 更强大
1、基于函数的超时设置
@pytest.mark.timeout(15)
def test_foo():print("timeout test")sleep(10)
2、命令行参数执行
pytest --timeout=2
3、存放到pytest.ini,直接执行pytest
[pytest] timeout = 2
二. 基于python方法的超时:pip install func_timeout
适用于对于参数化用例,每组参数需要的超时限制
from func_timeout import func_set_timeout, FunctionTimedOut
@func_set_timeout(2.5)
def mytest2():print("Start")for i in range(1, 10):print("%d seconds have passed" % i)time.sleep(10)
if __name__ == '__main__':try:mytest2()except FunctionTimedOut as e:print('mytest2:::', e)