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

本文介绍如何使用pytest单元测试框架生成不同类型的测试报告,包括普通文本、JunitXML、URL及HTML格式的报告,适用于持续集成工具的数据读取及美观展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From: https://www.cnblogs.com/fnng/p/4768239.html

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

2015-08-29 00:40 by 虫师, ... 阅读, 0 评论, 收藏, 编辑

 

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

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

复制代码
#coding=utf-8

class TestClass:

    def test_one(self): x = "this" assert "h" in x def 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_two
 self = <test_class.TestClass instance at 0x000000000307C788> def test_two(self): x = "hello" > assert x == "hi" E assert 'hello' == 'hi' E - hello E + hi test_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 - hello E + hi test_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文件,打开测试文件:

转载于:https://www.cnblogs.com/Raul2018/p/9760035.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值