zoukankan      html  css  js  c++  java
  • Pytest系列(11)- 失败重跑插件pytest-rerunfailures的详细使用

    转自:https://www.cnblogs.com/poloyy/

    一、环境前提

    以下先决条件才能使用pytest-rerunfailures

    • Python 3.5, 最高 3.8, or PyPy3
    • pytest 5.0或更高版本

    安装插件

    pip3 install pytest-rerunfailures -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
    

    三、提前了解重点

    命令行参数:--reruns n(重新运行次数),--reruns-delay m(等待运行秒数)

    装饰器参数:reruns=n(重新运行次数),reruns_delay=m(等待运行秒数)

    四、重新运行所有失败的用例

    ​ 要重新运行所有测试失败,使用 --reruns 命令行选项,并指定要运行测试的最大次数:`

    pytest --reruns 5 -s 
    

    4.1 知识点

    ​ 运行失败的fixture或setup_class也将重新执行

    4.2 添加重新运行的延时

    ​ 要在两次重试之间增加延迟时间,使用 --reruns-delay 命令行选项,指定下次测试重新开始之前等待的秒数

    pytest --reruns 5 --reruns-delay 10 -s
    

    五、重新运行指定的测试用例

    ​ 要将单个测试用例添加flaky装饰器 @pytest.mark.flaky(reruns=5) ,并在测试失败时自动重新运行,需要指定最大重新运行的次数

    5.1 例子

    import pytest
    
    @pytest.mark.flaky(reruns=5)
    def test_example():
        import random
        assert random.choice([True, False, False])
    

    ​ 执行结果

    collecting ... collected 1 item
    
    11_reruns.py::test_example RERUN                                         [100%]
    11_reruns.py::test_example PASSED                                        [100%]
    
    ========================= 1 passed, 1 rerun in 0.05s ==========================
    

    5.2 同样的,这个也可以指定重新运行的等待时间

    @pytest.mark.flaky(reruns=5, reruns_delay=2)
    def test_example():
        import random
        assert random.choice([True, False, False])
    

    注意事项:

    如果指定了用例的重新运行次数,则在命令行添加--reruns对这些用例是不会生效的

    六、兼容性问题

    • 不可以和fixture装饰器一起使用: @pytest.fixture()
    • 该插件与 pytest-xdist--looponfail 标志不兼容
    • 该插件与核心 --pdb标志不兼容
  • 相关阅读:
    关于linux下配置python3的virtualenvwrapper
    python-爬图小样
    C++反汇编学习笔记(五)
    C++反汇编学习笔记(四)
    C++反汇编学习笔记(三)
    OllyDebug调试技巧(三)
    C++反汇编学习笔记(二)
    OllyDebug调试技巧(二)
    OllyDebug调试技巧(一)
    PE结构学习笔记(十一)
  • 原文地址:https://www.cnblogs.com/dongye95/p/14015949.html
Copyright © 2011-2022 走看看