1.命令行运行
pytest --reruns 3 --reruns-delay 1 <文件路径>
这条命令的意思是:每次测试失败时,最多会重试 3 次,重试之间会有 1 秒的延迟。
2.通过装饰器启用自动重试
import pytest
@pytest.mark.flaky(reruns=3, reruns_delay=1)
def test_example():
assert False # 这个测试会失败并被重试 3 次
3.配置文件 pytest.ini
[pytest]
addopts = --reruns 3 --reruns-delay 1