pytest 常用插件的使用
生成HTML报告的插件:pytest-html
使用步骤:
- 为第三方插件,使用前需要先安装包
- 修改配置文件,增加--html参数指定HTML报告生成的路径
[pytest]
testpaths = case
addopts = -s --html=case/report/report.html
python_files = test*.py
python_classes = Test*
python_functions = test*
- 命令行执行pytest即可
控制测试函数执行顺序的插件:pythest-ordering
使用步骤:
1.安装第三方包
2.使用@pytest.mark.run(order=num) 控制函数执行次序
3. 命令行执行pytest即可
# 通过pytest-ordering 插件控制函数的执行顺序
import pytest
class TestDemo1:
# 需要注意的是 run(order=3) 没有提示需要手敲
# order 支持正数、负数、正数和负数混合使用
# 正数:数字越小,次序越优先
# 负数:数字越小,次序越优先
# 正负混合:正数按照次序执行,最后执行负数
# 同时也可以作用与class
@pytest.mark.run(order=3)
def test_method1(self):
print("this is method1....")
@pytest.mark.run(order=2)
def test_method2(self):
print("this is method2....")
@pytest.mark.run(order=1)
def test_method3(self):
print("this is method3....")
结果:
=============================================================== test session starts ===============================================================
platform win32 -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: E:\PyProject\demo\pytest, configfile: pytest.ini, testpaths: .
plugins: html-3.1.1, metadata-1.11.0, ordering-0.6
collected 3 items
04-pytest-order-插件.py this is method3....
.this is method2....
.this is method1....
.
---------------------------------- generated html file: file://E:\PyProject\demo\pytest\case\report\report.html -----------------------------------
================================================================ 3 passed in 0.02s ================================================================
失败重跑插件:pytest-rerunfailures
配置文件:
应用场景:自动化测试可能会使用网络,如果网络不好可能最终会使脚本不通过,像这种情况不是脚本本身的问题,那么就可以使用失败重试的插件,当失败后尝试最次运行
使用步骤:
1.安装包
2.修改配置文件,加入参数--reruns n
[pytest]
testpaths = .
addopts = -s
--html=case/report/report.html
--reruns 1
# python_files = 04*.py
python_files = 05_pytest_rerun.py
python_classes = Test*
python_functions = test*
=============================================================== test session starts ===============================================================
platform win32 -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: E:\PyProject\demo\pytest, configfile: pytest.ini, testpaths: .
plugins: html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2
> print(1 / 0)
E ZeroDivisionError: division by zero
05_pytest_rerun.py:4: ZeroDivisionError
---------------------------------- generated html file: file://E:\PyProject\demo\pytest\case\report\report.html -----------------------------------
============================================================= short test summary info =============================================================
FAILED 05_pytest_rerun.py::TestDemo::test_method - ZeroDivisionError: division by zero
=========================================================== 1 failed, 1 rerun in 0.07s ============================================================
命令行:
-reruns n(重新运行次数)
–reruns-delay m(等待运行秒数)
装饰器:
# reruns:重跑次数
# reruns_delay: 重跑间隔,单位秒
@pytest.mark.flaky(reruns=3, reruns_delay=1)
def test_01():
assert 1 == 2
if __name__ == '__main__':
pytest.main(['-n=2'])
结果:
PS D:\soft\pythonProject\demo> pytest
================================================================================= test session starts ==================================================================================
platform win32 -- Python 3.10.4, pytest-7.2.0, pluggy-1.0.0
rootdir: D:\soft\pythonProject\demo
collected 12 items
collected 12 items
test_demo3.py RRRF [ 8%]
test_demo3.py RRRF [ 8%]
test_scripts\test_demo.py . [ 16%]
======================================================================================= FAILURES =======================================================================================
_______________________________________________________________________________________ test_01 ________________________________________________________________________________________
@pytest.mark.flaky(reruns=3, reruns_delay=1)
def test_01():
> assert 1 == 2
E assert 1 == 2
test_demo3.py:8: AssertionError
=============================================================================== short test summary info ================================================================================
FAILED test_demo3.py::test_01 - assert 1 == 2
======================================================================== 1 failed, 11 passed, 3 rerun in 3.52s =========================================================================
PS D:\soft\pythonProject\demo>
测试报告插件 allure-pytest
Allure 是一个开源的 、灵活、轻量级、支持多语言的测试报告工具,基于JAVA语言开发,因此要具备JAVA环境
下载地址:https://github.com/allure-framework/allure2
官方文档:https://docs.qameta.io/allure-report/frameworks/python/pytest
下载解压后将bin目录配置的到环境变量
使用案例:
测试类:
# test_1_storm.py
import pytest, allure
@allure.feature("测试场景1") # 标记场景1
class TestDemo(object):
@allure.story('测试用例1-1') # 标记测试用例
@allure.severity('trivial') # 标记用例级别
def test_1_1(self):
a = 1 + 1
assert a == 2
@allure.story('测试用例1-2')
@allure.severity('critical')
@allure.step('用例2: 重要步骤')
def test_1_2(self):
assert 2 == 2
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir=./report/'])
执行后会在report文件夹生成一些JSON文件:
# 将生成的JSON文件转化为html报告
# allure generate --clean dir -o target_dir
# dir为存放json文件的路径,target_dir为生成html 报告的路径, 不加-o 参数默认生成到allure-report目录
E:\PyProject\homework\20220127>allure generate --clean report
Report successfully generated to allure-report
回到pycharm,已经生成报告:
问题1:
在外部打开后一直显示loadding:
解决:
allure open html
并发执行插件pytest-xdist
https://blog.csdn.net/wangmcn/article/details/121080902
pytest-sugar
作用:显示彩色和进度条
安装:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pytest-sugar
使用pytest-sugar 插件后,通过的测试用例后面会打勾,失败的测试用例后面会打叉,而且屏幕右侧会实时显示进度条
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/15720123.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!