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

2023-02-20 13:20:28 浏览数 (1)

1 简介

  • 用例失败重跑可以使用插件pytest-rerunfailures来实现;
  • pytest-rerunfailures有环境要求:

Python 3.5-3.8, or PyPy3 pytest 5.0或更高版本

  • 查看下自己的版本,如下:
代码语言:python代码运行次数: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.
>>>
代码语言:python代码运行次数:0复制
C:UsersAdministrator>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:python37libsite-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:UsersAdministrator>

2 插件pytest-rerunfailures安装

代码语言:python代码运行次数:0复制
pip install pytest-rerunfailures
代码语言:python代码运行次数:0复制
C:UsersAdministrator>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:python37libsite-packages (from pytest-rerunfailures) (2.1.1)
Requirement already satisfied: pytest>=5.3 in d:python37libsite-packages (from pytest-rerunfailures) (6.2.4)
Requirement already satisfied: packaging>=17.1 in d:python37libsite-packages (from pytest-rerunfailures) (20.8)
Requirement already satisfied: zipp>=0.5 in d:python37libsite-packages (from importlib-metadata>=1->pytest-rerunfailures) (1.2.0)
Requirement already satisfied: pyparsing>=2.0.2 in d:python37libsite-packages (from packaging>=17.1->pytest-rerunfailures) (2.4.7)
Requirement already satisfied: toml in d:python37libsite-packages (from pytest>=5.3->pytest-rerunfailures) (0.10.2)
Requirement already satisfied: pluggy<1.0.0a1,>=0.12 in d:python37libsite-packages (from pytest>=5.3->pytest-rerunfailures) (0.13.1)
Requirement already satisfied: atomicwrites>=1.0 in d:python37libsite-packages (from pytest>=5.3->pytest-rerunfailures) (1.4.0)
Requirement already satisfied: colorama in d:python37libsite-packages (from pytest>=5.3->pytest-rerunfailures) (0.4.4)
Requirement already satisfied: iniconfig in d:python37libsite-packages (from pytest>=5.3->pytest-rerunfailures) (1.1.1)
Requirement already satisfied: attrs>=19.2.0 in d:python37libsite-packages (from pytest>=5.3->pytest-rerunfailures) (20.3.0)
Requirement already satisfied: py>=1.8.2 in d:python37libsite-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 对这些用例是不会生效。
  • 比如:
代码语言:python代码运行次数:0复制
# -*- 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"])
代码语言:python代码运行次数:0复制
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%]
  • 增加重新运行的等待时间:
代码语言:python代码运行次数:0复制
@pytest.mark.flaky(reruns=3, reruns_delay=1)
def test_login():
    name = "zhang"
    assert name == "zhagnsan"

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

  • 使用 --reruns 命令行选项,并指定要运行测试的最大次数:
  • 运行失败的 fixturesetup_class 也将重新执行。
代码语言:python代码运行次数:0复制
pytest --reruns n --reruns-delay m -s
  • 比如:
代码语言:python代码运行次数:0复制
# -*- 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"])
代码语言:python代码运行次数:0复制
pytest --reruns 3 --reruns-delay 1 -s test_rerun.py
代码语言:python代码运行次数:0复制
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 =========================================

0 人点赞