pytest测试框架系列 - Pytest 失败重跑pytest-rerunfailures插件详解

本文介绍了pytest-rerunfailures插件的安装与使用,该插件允许在测试用例失败时进行重试,以应对服务器波动或网络不稳定导致的错误。通过命令行参数或装饰器可设置重试次数和间隔时间。同时,文章提到了插件的兼容性问题以及在不同配置下的优先级。
摘要由CSDN通过智能技术生成

前言

当服务器不稳定,偶尔出现波动、本地网络偶尔出现不稳定等情况,导致用例执行失败,这种情况大家都觉得用例标记失败不太合理,想要在用例执行失败后延时一些时间再次进行执行,如果超过3次仍然失败,则表明用例执行失败;所以就需要失败重跑功能。

pytest-rerunfailures 安装

  • 前提条件: pytest (>=5.3)python >=3.6
  • 安装:pip install pytest-rerunfailures
    pytest-rerunfailures 安装
  • 查看安装版本:pip show pytest-rerunfailures
    查看版本

pytest-rerunfailures 使用

  • 命令行参数: --reruns n(重新运行次数)–reruns-delay m(等待运行秒数)
  • 使用装饰器: @pytest.mark.flaky(reruns=5, reruns_delay=2)

在命令行参数进行用例失败重跑

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/8 23:22
# @Author  : king
# @File    :test_rerun.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

def add(a, b):
    return a + b

def test_rerun():
    assert add(1, 2) == 4

if __name__ == '__main__':
    pytest.main()

在命令行输入: pytest -v --reruns 2 --reruns-delay 5 test_rerun.py 失败用例重试2次,每次延时5秒
失败重试运行

使用装饰器的方式进行失败用例重跑

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/8 23:22
# @Author  : king
# @File    :test_rerun.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

def add(a, b):
    return a + b

@pytest.mark.flaky(reruns=2, reruns_delay=5)
def test_rerun():
    assert add(1, 2) == 4

if __name__ == '__main__':
    pytest.main()

在命令行输入: pytest -v test_rerun.py 失败用例重试2次,每次延时5秒
失败重跑

命令行参数和装饰器同时存在

说明:如果用例装饰器方式设置了重新运行次数,则在命令行添加–reruns对这些用例不会生效
示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/8 23:22
# @Author  : king
# @File    :test_rerun.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

def add(a, b):
    return a + b

@pytest.mark.flaky(reruns=1, reruns_delay=5)
def test_rerun():
    assert add(1, 2) == 4

if __name__ == '__main__':
    pytest.main()

在命令行输入: pytest -v --reruns 3 --reruns-delay 5 test_rerun.py 失败用例重试3次,每次延时5秒
在这里插入图片描述

兼容性

官网说明:https://pypi.org/project/pytest-rerunfailures/
Compatibility

  • This plugin may not be used with class, module, and package level fixtures.
  • This plugin is not compatible with pytest-xdist’s –looponfail flag.
  • This plugin is not compatible with the core –pdb flag.

翻译:

  • 这个插件不可以和 class, module, package 级别的fixture装饰器一起使用
  • 这个插件与 pytest-xdist--looponfail 标志不兼容
  • 这个插件与核心 --pdb 标志不兼容

总结

  • pytest-rerunfailures插件可以通过命令行参数 --reruns 2 --reruns-delay 5 进行使用,这个对于所有用例生效
  • pytest-rerunfailures插件使用装饰器装饰在用例上面,只对装饰的用例有效
  • 当时同时使用装饰器和命令行参数时,装饰器参数优先级高
  • 注意插件的兼容性

以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!

如果觉得文章不错,欢迎关注微信公众号,微信公众号每天优先推送相关测试技术文章
个人微信号

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试之路king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值