失败重跑插件pytest-rerunfailures的详细使用【pytest系列 10】

失败重跑插件pytest-rerunfailures的详细使用【pytest系列 10】,第1张

1、环境要求

需要满足以下条件才可以运行pytest-rerunfailures(未验证,不确定)

  • Python 3.6 ~ 3.9 或者 PyPy3
  • pytest 5.0 或者 更高版本
2、安装插件
pip install pytest-rerunfailures -i https://pypi.tuna.tsinghua.edu.cn/simple
3、命令行或者装饰器的方式重新运行失败的用例 方式一:命令行的方式
  • 要重新运行所有失败的用例,添加--reruns num参数,其中num表示最大运行次数:
    # 失败重运行最大次数为5次
    pytest --reruns 5
    
  • 要在两次重运行之间增加延时,则可以使用--reruns-delay参数,指定下一次重新运行之前需要等待的秒数:
    pytest --reruns 3 --reruns-delay 10
    
方式二:使用装饰器的方式
  • 在单个测试用例添加flaky装饰器,比如:@pytest.mark.flaky(reruns=5, reruns_delay=2) 表示重运行最大次数为5次,下一次开始前等待2s
  • 代码:
    import pytest
    
    @pytest.mark.flaky(reruns=3, reruns_delay=2)
    def test_01():
        import random
        assert random.choice([True, False, False, False])
    
  • 结果:
    ============================= test session starts ==============================
    collecting ... collected 1 item
    test_01.py::test_01 RERUN                                                [100%]
    test_01.py::test_01 PASSED                                               [100%]
    ========================== 1 passed, 1 rerun in 5.07s ==========================
    
4、总结
  • 命令行参数:--reruns n 重新运行次数,--reruns-delay m 等待运行秒数
  • 装饰器参数:reruns=n 重新运行次数,reruns_delay=m 等待运行秒数
  • 兼容性问题:
    • 不可以和fixture装饰器@pytest.fixture()一起使用
    • 该插件与pytest-xdist的--looponfail标志不兼容
    • 该插件与核心--pod表示不兼容

参考文章:https://www.cnblogs.com/poloyy/p/12687308.html

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/571234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存