• Pytest学习(十一)- 失败重跑插件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 
    

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

    重新运行的延时设置

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

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

    重新运行指定的测试用例

    添加flaky装饰器 @pytest.mark.flaky(reruns=5) ,并指定最大重新运行次数,示例代码如下:

    # -*- coding: utf-8 -*-
    # @Time    : 2020/11/25 20:36
    # @Author  : longrong.lang
    # @FileName: test_retry.py
    # @Software: PyCharm
    # @Cnblogs :https://www.cnblogs.com/longronglang
    from collections import Counter
    import random
    import pytest
    
    
    @pytest.mark.flaky(reruns=5)
    def test_retry1():
        n = random.randint(0, 9)
        print(f"
     输出随机数: {n} ")
        assert n == 2
    
    
    @pytest.mark.flaky(reruns=5)
    def test_retry2():
        assert random.choice([True, False, False])
    
    
    

    执行结果:

    对单个测试用例设置重新运行等待时间

    示例代码如下:

    @pytest.mark.flaky(reruns=5,reruns_delay=2)
    def test_retry1():
        n = random.randint(0, 9)
        print(f"
     输出随机数: {n} ")
        assert n == 2
    
    

    运行结果:

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

    兼容性问题

    • 不可以和fixture装饰器一起使用: @pytest.fixture()
    • 该插件与pytest-xdist的 --looponfail 标志不兼容
    • 该插件与核心--pdb标志不兼容

    系列参考文章:
    https://www.cnblogs.com/poloyy/category/1690628.html

    优秀不够,你是否无可替代

    软件测试交流QQ群:721256703,期待你的加入!!

    欢迎关注我的微信公众号:软件测试君


  • 相关阅读:
    JAVA 大数相加 POJ 1503
    Gao the Grid ZOJ 3647 数三角形
    为Layouts中的页面应用站点母版页的方法
    跨页面传值
    SharePoint Designer + InfoPath 无代码工作流设计实例
    自定义moss主题样式
    修改sharepoint文档库文件类型显示图标
    Workflow成功案例展示BingoSoft
    Walkthrough: 应用EventHandle开发的一个发邮件的小项目
    InfoPath + Workflow + MOSS
  • 原文地址:https://www.cnblogs.com/longronglang/p/14039119.html
Copyright © 2020-2023  润新知