当前位置: 首页 > news >正文

Python单元测试框架之pytest -- 生成测试报告

 继续pytest单元测试框架的学习,pytest可以生成多种类型的测试报告。这一节就来学习pytest如何生成测试报告。

零基础1小时快速入门pytest自动化测试教程,全套项目框架实战

  创建test_calss.py 测试用例文件,这里以测试该文件为例。

#coding=utf-8class TestClass:def test_one(self):x = "this"assert "h" in xdef test_two(self):x = "hello"assert x == "hi"

生成resultlog文件                                                         

 创建普通的结果文件:

> py.test test_class.py  --resultlog=./log.txt

  指定当前路径下生成log.txt文件,打开文件,内容如下:

复制代码

. test_class.py::TestClass::()::test_one
F test_class.py::TestClass::()::test_twoself = <test_class.TestClass instance at 0x000000000307C788>def test_two(self):x = "hello">           assert x == "hi"E           assert 'hello' == 'hi'E             - helloE             + hitest_class.py:11: AssertionError

复制代码

生成JunitXML文件                        

> py.test test_class.py  --junitxml=./log.xml

  同样指定在当前目录下生成log.xml文件,打开文件内容如下:

复制代码

<?xml version="1.0" encoding="utf-8"?>
<testsuite errors="0" failures="1" name="pytest" skips="0" tests="2" time="0.015"><testcase classname="test_class.TestClass" name="test_one" time="0.0"/><testcase classname="test_class.TestClass" name="test_two" time="0.00300002098083"><failure message="assert &apos;hello&apos; == &apos;hi&apos;- hello+ hi">self = &lt;test_class.TestClass instance at 0x000000000309C948&gt;def test_two(self):x = &quot;hello&quot;&gt;           assert x == &quot;hi&quot;E           assert &apos;hello&apos; == &apos;hi&apos;E             - helloE             + hitest_class.py:11: AssertionError</failure></testcase>
</testsuite>

复制代码

  创建这样的XML文件有有什么用? 主要是为了方便Jenkin或其它的持续集成工具俱读取。

创建测试用例的URL                                                

> py.test test_class.py  --pastebin=all

复制打印结果最后生成的session-log测试报告链接到浏览器:

https://bpaste.net/show/4815ce13c164

这样的结果展示将非常友好。

当然,你也可以只选择展示faile的测试用例

> py.test test_class.py  --pastebin=failed

生成html测试报告                              

  当然,更多时候,我们希望pytest能生成漂亮的测试报告。这需要安装pytest的扩展--pytest-html。

> pip install pytest-html     # 通过pip安装pytest-html

cmd命令提示符下执行测试文件:

>py.test test_class.py --html=./report.html

指定在当前目录下生成report.html文件,打开测试文件:


文章转载自:

http://bHtcE13v.xhkgL.cn
http://NscrC0b4.xhkgL.cn
http://EcVmVdOM.xhkgL.cn
http://YK4cZXhk.xhkgL.cn
http://XHospDwu.xhkgL.cn
http://BPQ46vjr.xhkgL.cn
http://dPP75VDV.xhkgL.cn
http://As5l8vnm.xhkgL.cn
http://44Yvi1Jp.xhkgL.cn
http://KGf6Vsys.xhkgL.cn
http://FwOvQySI.xhkgL.cn
http://5gv9uI1L.xhkgL.cn
http://oXrALPAV.xhkgL.cn
http://wQDCXQX6.xhkgL.cn
http://JWY2J07p.xhkgL.cn
http://OXIMGEqY.xhkgL.cn
http://q756Wim6.xhkgL.cn
http://JUVSPdSF.xhkgL.cn
http://swry068p.xhkgL.cn
http://j68PFYKM.xhkgL.cn
http://RbPSRXSN.xhkgL.cn
http://q1zx00AU.xhkgL.cn
http://AYbGcBtM.xhkgL.cn
http://SSqyXYna.xhkgL.cn
http://pACMHbfo.xhkgL.cn
http://2DrW5LJm.xhkgL.cn
http://MIt0EZ71.xhkgL.cn
http://cL4KkLye.xhkgL.cn
http://pv2y1mZd.xhkgL.cn
http://hzEKBywB.xhkgL.cn
http://www.dtcms.com/a/386275.html

相关文章:

  • 使用HBuilderX新建uniapp项目
  • 医疗行业安全合规数据管理平台:构建高效协作与集中化知识沉淀的一体化解决方案
  • 从一次鼠标点击窥探操作系统内核:中断、驱动、IPC与内存安全的奇幻之旅
  • 【超详细】C#的单例模式
  • 加快 NoETL 数据工程实践, Aloudata 荣登《2025 中国数智化转型升级创新服务企业》榜单
  • 香港服务器CN2带宽价格多少钱?很贵吗?
  • 180 课时吃透 Go 语言游戏后端系列1:第一个Go程序
  • MSI 与 IOAPIC LAPIC 如何协作,操作系统如何初始化和使用他们
  • 数据库优化(六)安全字段脱敏设计—东方仙盟金丹期
  • java21学习笔记
  • 大厂综合题库解析
  • 算法奇妙屋(2)-模拟
  • 贪心算法应用:区间调度问题详解
  • js中异步编程的实现方式【详细】
  • 详解 ArduPilot:开源无人机自动驾驶系统的全方位解析
  • 分页查询:时间筛选+日期筛选+增加queryWrapper 筛选条件
  • 通透理清三级缓存--看Spring是如何解决循环依赖的
  • 【08】AI辅助编程完整的安卓二次商业实战-修改消息聊天框背景色-触发聊天让程序异常终止bug牵涉更多聊天消息发送优化处理-优雅草卓伊凡
  • 查看 Docker 守护进程日志
  • 第11章 [特殊字符]️Hutool 常用工具类
  • 【MySQL|第十篇】总结篇——各种命令集合
  • npm : 无法加载文件 d:\nvm4w\nodejs\npm.ps1,
  • 贪心算法应用:活动选择问题详解
  • C++ 模板:以简御繁-5/5
  • AI大模型学习(6)Yolo V8神经网络的基础应用
  • 【完整源码+数据集+部署教程】残疾人和正常人识别图像分割系统: yolov8-seg-act
  • 深度学习:从概念到实践,开启智能时代新篇章
  • 构建AI智能体:三十五、决策树的核心机制(一):刨根问底鸢尾花分类中的参数推理计算
  • 美创科技入选 2025 年度省级场景型数字化服务商!
  • 《COD21》新赛季海量更新:《忍者神龟》联动上线!