加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Pytest学习(十一)- 失败重跑插件pytest-rerunfailures的使用

发布时间:2020-12-14 14:47:50 所属栏目:百科 来源:网络整理
导读:环境依赖 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(等待

环境依赖

  • 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 输出随机数: {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 输出随机数: {n} ")
    assert n == 2

运行结果:

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

兼容性问题

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读