pytest一些常见的插件

Pytest拥有丰富的插件架构,超过800个以上的外部插件和活跃的社区,在PyPI项目中以“ pytest- *”为标识。

本篇将列举github标星超过两百的一些插件进行实战演示。

插件库地址:http://plugincompat.herokuapp.com/


1、pytest-html:用于生成HTML报告

一次完整的测试,测试报告是必不可少的,但是pytest自身的测试结果过于简单,而pytest-html正好可以给你提供一份清晰报告。

安装:
pip install -U pytest-html
用例:

# test_sample.py
import pytest
# import time# 被测功能
def add(x, y):# time.sleep(1)return x + y# 测试类
class TestLearning:data = [[3, 4, 7],[-3, 4, 1],[3, -4, -1],[-3, -4, 7],]@pytest.mark.parametrize("data", data)def test_add(self, data):assert add(data[0], data[1]) == data[2]

运行:

E:\workspace-py\Pytest>pytest test_sample.py --html=report/index.html
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        test_sample.py ...F                                                                                                                                                [100%]=============================================================================== FAILURES ================================================================================
_____________________________________________________________________ TestLearning.test_add[data3] ______________________________________________________________________self = <test_sample.TestLearning object at 0x00000000036B6AC8>, data = [-3, -4, 7]@pytest.mark.parametrize("data", data)def test_add(self, data):
>       assert add(data[0], data[1]) == data[2]
E       assert -7 == 7
E        +  where -7 = add(-3, -4)test_sample.py:20: AssertionError
------------------------------------------------- generated html file: file://E:\workspace-py\Pytest\report\index.html --------------------------------------------------
======================================================================== short test summary info ========================================================================
FAILED test_sample.py::TestLearning::test_add[data3] - assert -7 == 7
====================================================================== 1 failed, 3 passed in 0.14s ======================================================================

运行完,会生产一个html文件 和 css样式文件夹assets,用浏览器打开html即可查看清晰的测试结果。

 

后面我将会更新更加清晰美观的测试报告插件: allure-python


2、pytest-cov:用于生成覆盖率报告

在做单元测试时,代码覆盖率常常被拿来作为衡量测试好坏的指标,甚至,用代码覆盖率来考核测试任务完成情况。

安装:
pip install -U pytest-cov
 运行:

E:\workspace-py\Pytest>pytest --cov=.
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        test_sample.py ....                                                                                                                                                [100%]----------- coverage: platform win32, python 3.7.3-final-0 -----------
Name             Stmts   Miss  Cover
------------------------------------
conftest.py          5      3    40%
test_sample.py       7      0   100%
------------------------------------
TOTAL               12      3    75%=========================================================================== 4 passed in 0.06s ===========================================================================


3、pytest-xdist:实现多线程、多平台执行

通过将测试发送到多个CPU来加速运行,可以使用-n NUMCPUS指定具体CPU数量,或者使用-n auto自动识别CPU数量并全部使用。

安装:
pip install -U pytest-xdist
用例:

# test_sample.py
import pytest
import time# 被测功能
def add(x, y):time.sleep(3)return x + y# 测试类
class TestAdd:def test_first(self):assert add(3, 4) == 7def test_second(self):assert add(-3, 4) == 1def test_three(self):assert add(3, -4) == -1def test_four(self):assert add(-3, -4) == 7

 运行:

E:\workspace-py\Pytest>pytest test_sample.py
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        test_sample.py ....                                                                                                                                                [100%]========================================================================== 4 passed in 12.05s ===========================================================================E:\workspace-py\Pytest>pytest test_sample.py -n auto
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, forked-1.3.0, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
gw0 [4] / gw1 [4] / gw2 [4] / gw3 [4]
....                                                                                                                                                               [100%]
=========================================================================== 4 passed in 5.35s ===========================================================================E:\workspace-py\Pytest>pytest test_sample.py -n 2
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, forked-1.3.0, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
gw0 [4] / gw1 [4]
....                                                                                                                                                               [100%]
=========================================================================== 4 passed in 7.65s ===========================================================================

上述分别进行了未开多并发、开启4个cpu、开启2个cpu,从运行耗时结果来看,很明显多并发可以大大缩减你的测试用例运行耗时。


4、pytest-rerunfailures:实现重新运行失败用例

 我们在测试时可能会出现一些间接性故障,比如接口测试遇到网络波动,web测试遇到个别插件刷新不及时等,这时重新运行则可以帮忙我们消除这些故障。

 安装:
pip install -U pytest-rerunfailures
运行:

E:\workspace-py\Pytest>pytest test_sample.py --reruns 3
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        test_sample.py ...R                                                                                                                                                [100%]R[100%]R [100%]F [100%]=============================================================================== FAILURES ================================================================================
___________________________________________________________________________ TestAdd.test_four ___________________________________________________________________________self = <test_sample.TestAdd object at 0x00000000045FBF98>def test_four(self):
>       assert add(-3, -4) == 7
E       assert -7 == 7
E        +  where -7 = add(-3, -4)test_sample.py:22: AssertionError
======================================================================== short test summary info ========================================================================
FAILED test_sample.py::TestAdd::test_four - assert -7 == 7
================================================================= 1 failed, 3 passed, 3 rerun in 0.20s ==================================================================

如果你想设定重试间隔,可以使用 --rerun-delay 参数指定延迟时长(单位秒); 

如果你想重新运行指定错误,可以使用 --only-rerun 参数指定正则表达式匹配,并且可以使用多次来匹配多个。

pytest --reruns 5 --reruns-delay 1 --only-rerun AssertionError --only-rerun ValueError

如果你只想标记单个测试失败时自动重新运行,可以添加 pytest.mark.flaky() 并指定重试次数以及延迟间隔。

@pytest.mark.flaky(reruns=5, reruns_delay=2)
def test_example():import randomassert random.choice([True, False])


5、pytest-randomly:实现随机排序测试

测试中的随机性非常越大越容易发现测试本身中隐藏的缺陷,并为你的系统提供更多的覆盖范围。

安装:
pip install -U pytest-randomly
运行:

E:\workspace-py\Pytest>pytest test_sample.py
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
Using --randomly-seed=3687888105
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, cov-2.10.1, html-3.0.0, randomly-3.5.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        test_sample.py F...                                                                                                                                                [100%]=============================================================================== FAILURES ================================================================================
___________________________________________________________________________ TestAdd.test_four ___________________________________________________________________________self = <test_sample.TestAdd object at 0x000000000567AD68>def test_four(self):
>       assert add(-3, -4) == 7
E       assert -7 == 7
E        +  where -7 = add(-3, -4)test_sample.py:22: AssertionError
======================================================================== short test summary info ========================================================================
FAILED test_sample.py::TestAdd::test_four - assert -7 == 7
====================================================================== 1 failed, 3 passed in 0.13s ======================================================================E:\workspace-py\Pytest>pytest test_sample.py
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
Using --randomly-seed=3064422675
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, forked-1.3.0, html-3.0.0, randomly-3.5.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        test_sample.py ...F                                                                                                                                                [100%]=============================================================================== FAILURES ================================================================================
___________________________________________________________________________ TestAdd.test_four ___________________________________________________________________________self = <test_sample.TestAdd object at 0x00000000145EA940>def test_four(self):
>       assert add(-3, -4) == 7
E       assert -7 == 7
E        +  where -7 = add(-3, -4)test_sample.py:22: AssertionError
======================================================================== short test summary info ========================================================================
FAILED test_sample.py::TestAdd::test_four - assert -7 == 7
====================================================================== 1 failed, 3 passed in 0.12s ======================================================================

这功能默认情况下处于启用状态,但可以通过标志禁用(假如你并不需要这个模块,建议就不要安装)。

pytest -p no:randomly

如果你想指定随机顺序,可以通过 --randomly-send 参数来指定,也可以使用 last 值来指定沿用上次的运行顺序。

pytest --randomly-seed=4321
pytest --randomly-seed=last

6、其他活跃的插件

还有一些其他功能性比较活跃的、一些专门为个别框架所定制的、以及为了兼容其他测试框架,这里暂不做演示,我就简单的做个列举:

pytest-django:用于测试Django应用程序(Python Web框架)。

pytest-flask:用于测试Flask应用程序(Python Web框架)。

pytest-splinter:兼容Splinter Web自动化测试工具。

pytest-selenium:兼容Selenium Web自动化测试工具。

pytest-testinfra:测试由Salt,Ansible,Puppet, Chef等管理工具配置的服务器的实际状态。

pytest-mock:提供一个mock固件,创建虚拟的对象来实现测试中个别依赖点。

pytest-factoryboy:结合factoryboy工具用于生成各式各样的数据。

pytest-qt:提供为PyQt5和PySide2应用程序编写测试。

pytest-asyncio:用于使用pytest测试异步代码。

pytest-bdd:实现了Gherkin语言的子集,以实现自动化项目需求测试并促进行为驱动的开发。

pytest-watch:为pytest提供一套快捷CLI工具。

pytest-testmon:可以自动选择并重新执行仅受最近更改影响的测试。

pytest-assume:用于每个测试允许多次失败。

pytest-ordering:用于测试用例的排序功能。

pytest-sugar:可立即显示失败和错误并显示进度条。

pytest-dev/pytest-repeat:可以重复(可指定次数)执行单个或多个测试。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/141193.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

数据库——理论基础

目录 1.1 什么是数据库 1.2 数据库管理系统&#xff08;DBMS&#xff09; 1.3 数据库和文件系统的区别 1.4 数据库的发展史 1.5常见的数据库 1.5.1关系型数据库 1.5.2 非关系型数据库 1.6 DBMS支持的数据模型 1.1 什么是数据库 数据&#xff1a;描述事物的符号记录 数…

opencv实现仿射变换和透射变换

##1&#xff0c; 什么是仿射变换&#xff1f; 代码实现 import numpy as np import cv2 as cv import matplotlib.pyplot as plt#设置字体 from pylab import mpl mpl.rcParams[font.sans-serif] [SimHei]#图像的读取 img cv.imread("lena.png")#仿射变换 row…

clickhouse简单安装部署

目录 前言(来源于官方文档)&#xff1a; 一.下载并上传 1.下载地址&#xff1a;点我跳转下载 2.上传至Linux 二.解压和配置 1.解压顺序 注意&#xff1a;必须按照以下顺序解压&#xff0c;并且每解压一个都要执行该解压后文件的install/doinst.sh文件 解压步骤&#xff…

Mycat管理及监控

Mycat管理 -h 是你自己的ip地址 相关命令及含义 Mycat-eye(图形化界面监控) 仅限于Linux系统

基于Xml方式Bean的配置-初始化方法和销毁方法

SpringBean的配置详解 Bean的初始化和销毁方法配置 Bean在被实例化后&#xff0c;可以执行指定的初始化方法完成一些初始化的操作&#xff0c;Bean在销毁之前也可以执行指定的销毁方法完成一些操作&#xff0c;初始化方法名称和销毁方法名称通过 <bean id"userService…

Spring Boot 日志文件

前言 本篇博客主要介绍自定义的日志打印、日志的级别高低、如何保存日志等等..... 一、日志是什么&#xff1f;日志有什么用&#xff1f; 日志就是我们控制台上输出的内容&#xff0c;控制台上的输出的信息就是日志信息&#xff0c;如下所示&#xff1a; 日志有什么用&#x…

【Vue2.0源码学习】生命周期篇-挂载阶段

文章目录 1. 前言2. 挂载阶段分析3. 总结 1. 前言 模板编译阶段完成之后&#xff0c;接下来就进入了挂载阶段&#xff0c;从官方文档给出的生命周期流程图中可以看到&#xff0c;挂载阶段所做的主要工作是创建Vue实例并用其替换el选项对应的DOM元素&#xff0c;同时还要开启对…

c语言基础知识+OS+数据结构

c语言&#xff1a; memory section&#xff1a; .bss&#xff1a; uninitialized or zero-initialized global and static variables .data: initialized global and static variables .text: Read only, code and const C语言编译流程&#xff1a; pre-compiler: …

在创业公司,我靠它续命 ...

不知不觉就在新公司工作了一周&#xff0c;没有想象中那么难受。创业公司里没有复杂的人际关系&#xff0c;也没有无聊的会议&#xff0c;更没有复杂的流程。每天上班第一件事就是开个小会&#xff0c;可能是站着开&#xff0c;也可能是连麦开。大家简单过一下前一天的进度&…

vue-cli创建项目、vue项目目录结(运行vue项目)、ES6导入导出语法、vue项目编写规范

vue-cli创建项目、vue项目目录结构、 ES6导入导出语法、vue项目编写规范 1 vue-cli创建项目 1.1 vue-cli 命令行创建项目 1.2 使用vue-cli-ui创建 2 vue项目目录结构 2.1 运行vue项目 2.2 vue项目的目录结构 3 es6导入导出语法 4 vue项目编写规范 4.1 修改项目 4.2 以后…

LabVIEW崩溃后所产生的错误日志文件的位置

LabVIEW崩溃后所产生的错误日志文件的位置 LabVIEW开发环境刚刚崩溃&#xff0c;请问我如何访问崩溃后自动生成的日志文件&#xff1f; LabVIEW崩溃后产生的转储文件位于何处&#xff1f; 代码导致了LabVIEW崩溃&#xff0c;请问哪些文件可以帮助NI技术支持了解具体原因&…

腾讯mini项目-【指标监控服务重构】2023-08-24

今日已办 Jeager 功能 监控分布式工作流程并排除故障识别性能瓶颈追踪根本原因分析服务依赖关系 部署 部署 Deployment — Jaeger documentation (jaegertracing.io) 支持 clickhouse jaegertracing/jaeger-clickhouse: Jaeger ClickHouse storage plugin implementation …

Python之列表

标题 列表什么是列表列表的创建列表的删除列表的访问 列表的常用方法append()、insert()、extend()pop()、remove()、clear()count()、index()list()、 filter()、 reduce()、lambda() 列表支持的运算加法运算符乘法运算符*成员测试运算符in 内置函数对列表的操作列表推导式列表…

Python 逢七拍手小游戏2.0

"""逢七拍手游戏介绍&#xff1a;逢七拍手游戏的规则是&#xff1a;从1开始顺序数数&#xff0c;数到有7&#xff0c;或者是7的倍数时&#xff0c;就拍一手。例如&#xff1a;7、14、17......70......知识点&#xff1a;1、循环语句for2、嵌套条件语句if/elif/e…

tensorrt获取输入输出

利用Netron打开onnx&#xff0c;右边名字&#xff1a; int input_index engine->getBindingIndex("inout1.1");int output_index engine->getBindingIndex("191");

010_第一代软件开发(二)

第一代软件开发(二) 文章目录 第一代软件开发(二)项目介绍界面布局功能完善快照功能获取可用串口播放按键提示音 关键字&#xff1a; Qt、 Qml、 QSerialPort、 QPixmap、 QSoundEffect 项目介绍 欢迎来到我们的 QML & C 项目&#xff01;这个项目结合了 QML&#xff…

Java基础(一)——Hello World,8种数据类型,键盘录入

个人简介 &#x1f440;个人主页&#xff1a; 前端杂货铺 &#x1f64b;‍♂️学习方向&#xff1a; 主攻前端方向&#xff0c;正逐渐往全干发展 &#x1f4c3;个人状态&#xff1a; 研发工程师&#xff0c;现效力于中国工业软件事业 &#x1f680;人生格言&#xff1a; 积跬步…

C++项目:仿muduo库实现高性能高并发服务器

文章目录 一、实现目标二、前置知识&#xff08;一&#xff09;HTTP服务器1.概念 &#xff08;二&#xff09;Reactor模型&#xff1a;1.概念2.分类&#xff08;1&#xff09;单Reactor单线程&#xff1a;单I/O多路复用业务处理。&#xff08;2&#xff09;单Reactor多线程&…

ElementUI之登陆+注册

一.什么是ElementUI 二.ElementUI完成用户注册登录界面搭建 使用命令npm install element-ui -S&#xff0c;添加Element-UI模块 导依赖 建立登录和注册页面 ​编辑 配置样式 编写登录页面&#xff08;Login&#xff09; 编写注册页面&#xff08;reginter&#xff09; …

Elasticsearch(Es搜索(简单使用、全文查询、复合查询)、地理位置查询、特殊查询、聚合操作、桶聚合、管道聚合)

Elasticsearch&#xff08;三&#xff09;——Es搜索&#xff08;简单使用、全文查询、复合查询&#xff09;、地理位置查询、特殊查询、聚合操作、桶聚合、管道聚合 一、Es搜索 这里的 Es 数据博主自己上网找的&#xff0c;为了练习 Es 搜索。 1、Elasticsearch 搜索入门 …