Pytest - 失败用例重试插件:rerunfailures
pytest-rerunfailures 插件,功能是用例执行失败可以重试。
- python 3.6+
- pytest 5.3+
用例重试时,fixture
或 setup_class
也将被重新执行。
安装
$ pip install pytest-rerunfailures
使用
命令行使用
# 用例失败重试 3 次
$ pytest --reruns 3
# 用例失败重试 3 次,每次重试间隔时间为 2 秒
$ pytest --reruns=3 \
--reruns-delay=2
# 仅重试指定异常的用例
$ pytest --reruns=2 \
--only-rerun=AssertionError
# 指定多个异常
pytest --reruns 2 \
--only-rerun AssertionError \
--only-rerun ValueError
# 仅重试与指定异常不匹配的用例,仅重试非断言失败的用例
$ pytest --reruns 2 \
--rerun-except AssertionError
# 指定多个
$ pytest --reruns 2 \
--rerun-except AssertionError \
--rerun-except OSError
用例内使用装饰器
import pytest
"""执定重试次数"""
@pytest.mark.flaky(reruns=2)
def test_example():
assert False
"""指定重试间隔时间"""
@pytest.mark.flaky(reruns=2, reruns_delay=2)
def test_example():
assert False
"""指定重试条件"""
# linux 系统下开启重试, mac 执行不重试
@pytest.mark.flaky(reruns=2, condition=sys.platform.startswith("Linux"))
def test_example():
assert False
# 非 linux 系统开启重试,mac 执行重试生效
@pytest.mark.flaky(reruns=2, condition=not sys.platform.startswith("Linux"))
def test_example():
assert False
兼容性
- 不能与
fixture
一起使用 - 与
pytest-xdist –looponfail
自动监听功能不兼容 - 与 –pdb 调试功能不兼容
作者:ABEELAN
出处:https://www.cnblogs.com/abeelan/p/17203388.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!