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

举报
虫无涯 发表于 2023/02/08 09:01:29 2023/02/08
【摘要】 1 简介用例失败重跑可以使用插件pytest-rerunfailures来实现;pytest-rerunfailures有环境要求:Python 3.5-3.8, or PyPy3pytest 5.0或更高版本查看下自己的版本,如下:Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD...

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 =========================================

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。