目录
- 1、前言
- 2、使用
- (一)命令行或main函数中使用
- (二)全局配置中使用(推荐用法)
1、前言
有时候我们测试执行由于某些原因失败了,想要多执行几次,Pytest
可以使用pytest-rerunfailures
插件来实现。
安装方式:pip install pytest-rerunfailures
2、使用
(一)命令行或main函数中使用
pytest -v -s ./xxx.py --reruns 2
pytest.main(["-v","-s","xxx.py","--reruns=2"])
(二)全局配置中使用(推荐用法)
在pytest.ini
配置文件中addopts
添加reruns
参数
[pytest]
addopts = -s -v --reruns 2 --reruns-delay 2
testpaths = scripts
python_files = test_*.py
python_classes = Test*
python_functions = test*
说明:
--reruns 2
代表重跑次数
--reruns-delay 2
代表重跑间隔,单位秒
示例:
def test_1():print("测试1")assert Truedef test_2():print("测试2")assert Falsedef test_3():print("测试3")assert True"""
执行结果
mark/reruns/reruns.py::test_1 测试1
PASSED
mark/reruns/reruns.py::test_2 测试2
RERUN
mark/reruns/reruns.py::test_2 测试2
RERUN
mark/reruns/reruns.py::test_2 测试2
FAILED
mark/reruns/reruns.py::test_3 测试3
PASSED
"""