pytest框架进阶自学系列 | 结果分析及报告

书籍来源:房荔枝 梁丽丽《pytest框架与自动化测试应用》

一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴:pytest框架进阶自学系列 | 汇总_热爱编程的通信人的博客-CSDN博客


分析测试执行时间

获取执行最慢的10个测试用例:

默认情况下,可以使用-vv选项查看它们。

创建及定制JUnitXML格式的测试报告

测试报告是我们在测试过程中必不可少的一个环节,pytest通过这种形式提供报告的输出和定制。同样,因为重要,pytest的第三方插件allure已经将报告做到极致,并且与Jenkins有很好的集成。因此以下内容在实际工作中用得不多。如果真要用到的这里没有说明的方法请直接到pytest官网上阅读。

使用pytest--junitxml=path命令,可以在指定的path中创建一个能被Jenkins或者其他CI工具读取的XML格式的测试报告。

具体步骤如下:

(1)可以在项目的pytest.ini文件输入下面的信息:

[pytest]
junit_suite_name=pytest_chinese_doc

通过设置junit_suite_name的值,可以自定义xml文件中testsuite根节点的name信息,junit_suite_name是4.0版本新增的配置项。

(2)输入pytest -q --junitxml=report/test_one.xml test_nodeid.py::test_one并执行。

(3)在当前路径下的report下面可找到test_one.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite errors="0" failures="0" hostname="LENOVO-R720" name="pytest_chinese_doc" skipped="0" tests="1" time="0.012" timestamp="2023-07-06T21:22:18.763621"><testcase classname="test_nodeid" file="test_nodeid.py" line="2" name="test_one" time="0.000"></testcase></testsuite></testsuites>

(4)<testsuite>节点的name属性的值,变为我们所期望的pytest _chinese_doc。

(5)time属性表明测试用例执行的全部耗时,包含setup和teardown中的操作。

如果只想记录测试用例执行的时间,只需进行如下配置,大家可以自己实验:

[pytest]
junit_duration_report=call
  1. 在报告中为测试用例附加额外的子节点信息

使用record_property fixture为test_record_property用例添加一个额外的test_id,代码如下:

def test_record_property(record_property):
    record_property("test_id", 10010)
    assert 1

在报告中的表现为<property name="test_id" value="10010"/>:

<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite errors="0" failures="0" hostname="LENOVO-R720" name="pytest" skipped="0" tests="1" time="0.068" timestamp="2023-07-06T21:24:58.957717"><testcase classname="test_xml_report" file="test_xml_report.py" line="0" name="test_record_property" time="0.000"><properties><property name="test_id" value="10010"/></properties></testcase></testsuite></testsuites>

注意:变动后的报告可能不符合最新的JUnitXML模式检查规则,从而可能导致在某些CI工具上发生未知的错误。

  1. 在报告中为测试用例附加额外的属性信息

可以通过record_xml_attribute fixture为测试用例附加额外的属性,而不是通过record_property为其添加子节点。为测试用例添加一个test_id属性,并修改原先的classname属性,代码如下:

def test_record_property2(record_xml_attribute):
    record_xml_attribute('test_id', 10010)
    record_xml_attribute('classname', 'custom_classname')
    assert 1

在报告中的表现为<testcase classname="custom_classname"test_id="10010"...:

<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite errors="0" failures="0" hostname="LENOVO-R720" name="pytest" skipped="0" tests="1" time="0.085" timestamp="2023-07-06T21:29:39.548391"><testcase classname="custom_classname" file="test_xml_report.py" line="0" name="test_record_property2" test_id="10010" time="0.001"></testcase></testsuite></testsuites>

注意:record_xml_attribute目前是一个实验性的功能,未来可能被更强大的API所替代,但功能本身会被保留。变动后的报告可能不符合最新的JUnitXML模式检查规则,从而可能导致在某些CI工具上发生未知的错误。

  1. 在报告中为测试集附加额外的子节点信息

这部分内容涉及第3章内容,建议学完第3章后再参考查看。

这是pytest 4.5版本新增功能,可以通过自定义一个session作用域级别的fixture为测试集添加子节点信息,并且会作用于所有的测试用例,这个自定义的fixture需要调用另外一个record_testsuite_property fixture。

record_testsuite_property接收两个参数name和value以构成<property>标签,其中name必须为字符串,value会转换为字符串并进行XML转义。

代码如下:

import pytest

@pytest.fixture(scope="session")
def log_global_env_facts(record_testsuite_property):
    record_testsuite_property("EXECUTOR", "lindafang")
    record_testsuite_property("LOCATION", "HRB")
    
def test_record_property3(log_global_env_facts):
    assert 1

生成的测试报告表现在testsuite节点中,多了一个properties子节点,包含所有新增的属性节点,而且,它和所有的testcase节点是平级的:

<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite errors="0" failures="0" hostname="LENOVO-R720" name="pytest" skipped="0" tests="1" time="0.165" timestamp="2023-07-06T21:34:07.912877"><properties><property name="EXECUTOR" value="lindafang"/><property name="LOCATION" value="HRB"/></properties><testcase classname="test_xml_report" file="test_xml_report.py" line="7" name="test_record_property3" time="0.002"></testcase></testsuite></testsuites>

注意:这样生成的xml文件符合最新的xUnit标准,这点和record_property、record_xml_attribute正好相反。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值