背景
日常测试过程中,经常需要对失败测试用例进行retry,此时可使用pytest-rerunfailures插件。
安装
pip install pytest-rerunfailures
使用
- 装饰器
将单个测试用例添加flaky装饰器 @pytest.mark.flaky(reruns=3, reruns_delay=2) ,并在测试失败时自动重新运行,需要指定最大重新运行的次数,也可设置重新运行的等待时间
@pytest.mark.flaky(reruns=3, reruns_delay=2) # 最大重试次数3,等待时间为3s
def test_rerun(self):
a = 1
b = 2
assert a+b == 4
执行结果:
- 命令行
pytest --reruns 3 --reruns-delay 2 -s
注意
- 如果使用装饰器指定了重新运行次数,在命令行添加–reruns对这些用例不生效
- 不可以和fixture装饰器一起使用: @pytest.fixture()
- 该插件与pytest-xdist的 --looponfail 标志不兼容
- 该插件与核心–pdb标志不兼容