pytest学习和使用15-Pytest用例失败如何重跑?(pytest-rerunfailures的简单使用)
2023-03-07 09:47:49 时间
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 |
---|---|---|
命令行参数 |
|
|
装饰器参数 |
|
|
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
命令行选项,并指定要运行测试的最大次数: - 运行失败的
fixture
或setup_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 =========================================
相关文章
- 认识新的 AWS 勇士
- 新功能 — 由 Gaudi 加速器提供支持的用于训练深度学习模型的 EC2 实例
- 新增功能 — 配备 NVIDIA A10G Tensor Core GPU 的 EC2 实例 (G5)
- 由机器学习提供支持的 Amazon Connect 现新增呼叫总结功能
- 【Java】Java学习笔记之二----修饰符
- 新的 AWS 奖学金计划帮助代表性不足和服务不足的学生为 AI 和 ML 领域的职业做好准备
- 现已推出预览版 — Amazon SageMaker Studio Lab,一项具有机器学习 (ML) 功能的免费学习和实验服务
- 基于云原生构建的 HPC 平台演示
- 构建 ElastiCache for Redis 慢日志可视化平台
- 智能家居物联网平台之-设备状态管理
- 使用 Deep Learning AMI 快速实现 CUDA,cuDNN 和深度学习框架版本兼容
- 智能家居物联网平台之 —— 模组配网
- 利用Amazon EC2进一步降低DeepRacer训练成本
- 使用机器学习和 AWS Inferentia 扩缩广告验证
- 使用Amazon Web Services CDK,在云上构建DataOPS 平台
- 在Amazon EKS上部署Zeppelin和Spark分析平台
- 通过 SageMaker 与 Step Functions 实现机器学习的 CI/CD 方案
- 基于AWS machine learning bot 的 named-entity recognition (NER) 快速解决方案
- 【java8】java新特性(一)——全局观
- 介绍最新的 AWS 勇士 – 2021 年 6 月