pytest学习和使用15-Pytest用例失败如何重跑?(pytest-rerunfailures的简单使用)

简介: pytest学习和使用15-Pytest用例失败如何重跑?(pytest-rerunfailures的简单使用)

1 简介

  • 用例失败重跑可以使用插件pytest-rerunfailures来实现;
  • pytest-rerunfailures有环境要求:
Python 3.5-3.8, or PyPy3
pytest 5.0或更高版本
  • 查看下自己的版本,如下:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Administrator>pip show pytest
Name: pytest
Version: 6.2.4
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email:
License: MIT
Location: d:\python37\lib\site-packages
Requires: atomicwrites, attrs, colorama, importlib-metadata, iniconfig, packaging, pluggy, py, toml
Required-by: allure-pytest, pytest-cov, pytest-forked, pytest-html, pytest-metadata, pytest-ordering, pytest-xdist

C:\Users\Administrator>

2 插件pytest-rerunfailures安装

pip install pytest-rerunfailures
C:\Users\Administrator>pip install pytest-rerunfailures
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pytest-rerunfailures
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/89/d7/324c800be87ecf875d27070c19ced50f1eafde428eccf8c351b459e06714/pytest_rerunfailures-10.3-py3-none-any.whl (11 kB)
Requirement already satisfied: importlib-metadata>=1 in d:\python37\lib\site-packages (from pytest-rerunfailures) (2.1.1)
Requirement already satisfied: pytest>=5.3 in d:\python37\lib\site-packages (from pytest-rerunfailures) (6.2.4)
Requirement already satisfied: packaging>=17.1 in d:\python37\lib\site-packages (from pytest-rerunfailures) (20.8)
Requirement already satisfied: zipp>=0.5 in d:\python37\lib\site-packages (from importlib-metadata>=1->pytest-rerunfailures) (1.2.0)
Requirement already satisfied: pyparsing>=2.0.2 in d:\python37\lib\site-packages (from packaging>=17.1->pytest-rerunfailures) (2.4.7)
Requirement already satisfied: toml in d:\python37\lib\site-packages (from pytest>=5.3->pytest-rerunfailures) (0.10.2)
Requirement already satisfied: pluggy<1.0.0a1,>=0.12 in d:\python37\lib\site-packages (from pytest>=5.3->pytest-rerunfailures) (0.13.1)
Requirement already satisfied: atomicwrites>=1.0 in d:\python37\lib\site-packages (from pytest>=5.3->pytest-rerunfailures) (1.4.0)
Requirement already satisfied: colorama in d:\python37\lib\site-packages (from pytest>=5.3->pytest-rerunfailures) (0.4.4)
Requirement already satisfied: iniconfig in d:\python37\lib\site-packages (from pytest>=5.3->pytest-rerunfailures) (1.1.1)
Requirement already satisfied: attrs>=19.2.0 in d:\python37\lib\site-packages (from pytest>=5.3->pytest-rerunfailures) (20.3.0)
Requirement already satisfied: py>=1.8.2 in d:\python37\lib\site-packages (from pytest>=5.3->pytest-rerunfailures) (1.10.0)
Installing collected packages: pytest-rerunfailures
Successfully installed pytest-rerunfailures-10.3

3 参数说明

分类 参数1 参数2
命令行参数 --reruns n(重新运行次数) --reruns-delay m(等待运行秒数)
装饰器参数 reruns=n(重新运行次数) reruns_delay=m(等待运行秒数)

4 注意事项

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

5 重新运行指定测试用例

  • 要将单个测试用例添加flaky装饰器 @pytest.mark.flaky(reruns=5)
  • 并在测试失败时自动重新运行,需要指定最大重新运行的次数。
  • 如果指定了用例的重新运行次数,则在命令行添加 --reruns 对这些用例是不会生效。
  • 比如:
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_rerun.py
# 作用:用例失败重跑
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson


import pytest

@pytest.mark.flaky(reruns=3)
def test_login():
    name = "zhang"
    assert name == "zhagnsan"


if __name__ == '__main__':
    pytest.main(["-s", "test_rerun.py"])
test_rerun.py::test_login RERUN  [100%]
test_rerun.py::test_login RERUN  [100%]
test_rerun.py::test_login RERUN  [100%]
test_rerun.py::test_login FAILED [100%]
  • 增加重新运行的等待时间:
@pytest.mark.flaky(reruns=3, reruns_delay=1)
def test_login():
    name = "zhang"
    assert name == "zhagnsan"

6 重新运行所有失败的用例

  • 使用 --reruns 命令行选项,并指定要运行测试的最大次数:
  • 运行失败的 fixturesetup_class 也将重新执行。
pytest --reruns n --reruns-delay m -s
  • 比如:
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/12/27 
# 文件名称:test_rerun.py
# 作用:用例失败重跑
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson


import pytest

def test_login():
    name = "zhang"
    assert name == "zhagnsan"

def test_case01():
    sum = 5 + 5
    assert sum == 11

def test_case02():
    pwd = "123456"
    assert pwd == "12345678"


if __name__ == '__main__':
    pytest.main(["-s", "test_rerun.py"])
pytest --reruns 3 --reruns-delay 1 -s test_rerun.py
test_rerun.py RRRFRRRFRRRF

================================================== FAILURES ==================================================
_________________________________________________ test_login _________________________________________________

    def test_login():
        name = "zhang"
>       assert name == "zhagnsan"
E       AssertionError: assert 'zhang' == 'zhagnsan'
E         - zhagnsan
E         + zhang

test_rerun.py:14: AssertionError
________________________________________________ test_case01 _________________________________________________

    def test_case01():
        sum = 5 + 5
>       assert sum == 11
E       assert 10 == 11

test_rerun.py:18: AssertionError
________________________________________________ test_case02 _________________________________________________

    def test_case02():
        pwd = "123456"
>       assert pwd == "12345678"
E       AssertionError: assert '123456' == '12345678'
E         - 12345678
E         ?       --
E         + 123456

test_rerun.py:22: AssertionError
========================================== short test summary info ===========================================
FAILED test_rerun.py::test_login - AssertionError: assert 'zhang' == 'zhagnsan'
FAILED test_rerun.py::test_case01 - assert 10 == 11
FAILED test_rerun.py::test_case02 - AssertionError: assert '123456' == '12345678'
========================================= 3 failed, 9 rerun in 9.30s =========================================
目录
相关文章
|
29天前
|
运维 测试技术
实用指南:使用Pytest Allure测试框架添加用例失败截图
本文介绍了如何在使用`allure+pytest`进行软件测试时,通过`pytest_runtest_makereport`钩子函数自动捕获失败用例的截图。在`conftest.py`中定义钩子,当用例失败时,保存截图并附加到Allure测试报告中。测试代码示例展示了登录豆瓣的场景,测试失败时会自动生成截图。这种方法有助于快速理解和解决测试问题,提升测试效率和软件质量。
17 0
|
10月前
|
测试技术 C++ Python
【pytest】pytest的几种运行方式,尤其最后一种调试很方便
【pytest】pytest的几种运行方式,尤其最后一种调试很方便
|
10月前
|
测试技术
29-pytest-运行上次失败用例
29-pytest-运行上次失败用例
35-pytest-Hooks函数之统计测试结果
35-pytest-Hooks函数之统计测试结果
|
10月前
|
测试技术 Python
02-pytest-用例运行规则
02-pytest-用例运行规则
|
负载均衡 监控 测试技术
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
150 0
pytest学习和使用20-pytest如何进行分布式测试?(pytest-xdist)
|
自然语言处理 Java 测试技术
pytest学习和使用21-测试报告插件allure-pytest如何使用?
pytest学习和使用21-测试报告插件allure-pytest如何使用?
121 0
pytest学习和使用21-测试报告插件allure-pytest如何使用?
pytest学习和使用19-pytest断言失败后,怎样保持后续的断言继续执行?(pytest-assume)
pytest学习和使用19-pytest断言失败后,怎样保持后续的断言继续执行?(pytest-assume)
91 0
|
测试技术 Python
Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用
Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用
548 0
|
测试技术 Python
pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)
pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)
102 0
pytest学习和使用17-Pytest如何重复执行用例?(pytest-repeat)