zoukankan      html  css  js  c++  java
  • 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,期待你的加入!!

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


  • 相关阅读:
    android灭屏后调用binder通讯竟然影响了socket的POLL_OUT事件,怪事。
    B0宏
    从surfaceflinger历史变更谈截屏
    arm下dlsym返回的符号地址居然不是偶对齐的。
    SIGCHLD waitpid, 小心子进程结束事件被偷了
    root权限后,不要忘了还有selinux
    shell命令管道未读完阻塞了子进程,与等待其结束的父进程死"锁"。
    我对BP网络的简单的理解
    python 中的字符串格式化
    阿里云深度学习采坑记
  • 原文地址:https://www.cnblogs.com/longronglang/p/14039119.html
Copyright © 2011-2022 走看看