使用pytest的 reporting特性来生成报告!

1123 篇文章 44 订阅
847 篇文章 2 订阅
本文介绍了如何使用pytest及其插件如pytest-html、pytest-xml和pytest-json生成不同格式的测试报告,包括HTML、XML和JSON。同时展示了如何在报告中添加自定义字段和嵌入Matplotlib图表。
摘要由CSDN通过智能技术生成

特性

图片

1. HTML 报告:使用 pytest-html 插件,你可以生成 HTML 格式的测试报告。只需在项目的 pytest.ini 文件中添加以下内容:

[pytest]addopts = --html=report.html

然后,在运行 pytest 时,将会生成一个名为 report.html 的 HTML 文件,其中包含了测试用例的详细信息和结果。

2. XML 报告:使用 pytest-xml 插件,你可以生成 XML 格式的测试报告。同样,在项目的 pytest.ini 文件中添加以下内容:​​​​​​​

[pytest]addopts = --xml=report.xml

运行 pytest 后,将会生成一个名为 report.xml 的 XML 文件,可供其他工具或系统使用。

3. JSON 报告:使用 pytest-json 插件,你可以生成 JSON 格式的测试报告。在 pytest.ini 文件中添加以下内容:​​​​​​​

[pytest]addopts = --json=report.json

运行 pytest 后,将会生成一个名为 report.json 的 JSON 文件,包含了测试用例的相关信息。

4. 控制台报告:默认情况下,pytest 在控制台输出测试结果。你可以通过设置 pytest.ini 文件中的 verbosity 选项来控制报告的详细程度,例如:​​​​​​​

[pytest]verbosity = 2

这些 reporting 特性可以帮助你更好地了解测试的执行情况,并与其他团队成员或工具进行共享和分析。

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:691998057【暗号:csdn999】

如何在报告中添加自定义字段?

图片

要在 pytest 的报告中添加自定义字段,你可以使用 pytest-html 插件来生成 HTML 格式的报告,并在报告中添加自定义字段。以下是一个示例,展示了如何在 HTML 报告中添加自定义字段 Environment 和 Execution Time:​​​​​​​

import datetimefrom py.xml import htmlimport pytestimport time# 修改报告名称def pytest_html_report_title(report):    report.title = "接口自动化测试报告"# 添加环境项def pytest_configure(config):    config._metadata('测试人员') = 'emily'# 添加执行时间def pytest_html_results_table_header(cells):    cells.insert(0, html.th('用例编号'))    cells.insert(1, html.th('所属模块'))    cells.insert(2, html.th('用例名称'))    cells.insert(3, html.th('接口路径'))    cells.insert(5, html.th('执行时间', class_='sortable time', col='time'))    cells.pop(6)    cells.pop()# 获取测试节点def pytest_html_results_table_row(report, cells):    url = 'http://xxx.com'    testnode = report.nodeid.encode("utf-8").decode("unicode_escape")    caseid = testnode.split('-')(3)    cells.insert(0, html.td(caseid))    module = testnode.split('-')(2)    cells.insert(1, html.td(module))    casename = testnode.split('-')(1)    url = url+testnode.split('-')(4)(:-1)    cells.insert(2, html.td(casename))    cells.insert(3, html.td(url))    cells.insert(5, html.td(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), class_='col-time'))    cells.pop(6)    cells.pop()# 在运行测试之前执行的钩子函数@pytest.hookimpl(hookwrapper=True)def pytest_runtest_makereport(item, call):    outcome = yield    report = outcome.get_result()    report.casename = str(item.function.__code__.co_varnames)

首先,在 pytest_configure 函数中,使用 config._metadata 来添加一个名为 测试人员 的自定义字段,其值为 emily。接下来,在 pytest_html_results_table_header 函数中,向 HTML 报告的表头中添加了一个名为 执行时间 的新列。然后,在 pytest_html_results_table_row 函数中,从测试用例中获取相关信息,并将其插入到报告的行数据中。最后,使用 pytest_runtest_makereport 钩子函数来修改测试用例的名称,使其包含函数的参数名。

运行 pytest 命令后,将会生成一个名为 report.html 的 HTML 文件,其中包含了测试用例的详细信息和结果,并且包含了自定义字段 Environment 和 Execution Time。

如何在报告中添加图表?

图片

可以使用一些第三方库或工具来实现。以下是一种常见的方法,使用 Python 的 Matplotlib 库来生成图表并将其嵌入到 HTML 报告中:

1. 首先,确保你已经安装了 Matplotlib 库。

2. 在你的测试用例中,使用 Matplotlib 绘制图表,并将其保存为图像文件(例如 PNG 格式)。

3. 在 HTML 报告中,使用 HTML 和 CSS 来嵌入和显示图像。你可以在报告的适当位置添加  标签,并指定图像的路径。

以下是一个简单的示例,展示了如何在 HTML 报告中添加图表:​​​​​​​

import pytestimport matplotlib.pyplot as pltdef test_sample_function():    # 生成图表数据    x = [1, 2, 3, 4, 5]    y = [2, 4, 6, 8, 10]    # 绘制图表    plt.plot(x, y)    plt.xlabel('X 轴')    plt.ylabel('Y 轴')    plt.title('图表示例')    plt.savefig('chart.png')    # 断言图表文件存在    assert os.path.exists('chart.png')@pytest.mark.parametrize('param', [1, 2, 3])def test_with_params(param):    # 在这里使用参数进行测试# 修改报告名称def pytest_html_report_title(report):    report.title = "测试报告"# 添加图表到报告中def pytest_html_results_table_header(cells):    cells.insert(0, html.th('用例编号'))    cells.insert(1, html.th('所属模块'))    cells.insert(2, html.th('用例名称'))    cells.insert(3, html.th('图表', class_='sortable chart', col='chart'))# 获取测试节点def pytest_html_results_table_row(report, cells):    url = 'http://xxx.com'    testnode = report.nodeid.encode("utf-8").decode("unicode_escape")    caseid = testnode.split('-')(3)    cells.insert(0, html.td(caseid))    module = testnode.split('-')(2)    cells.insert(1, html.td(module))    casename = testnode.split('-')(1)    cells.insert(2, html.td(casename))    cells.insert(3, html.td(html.Img(src='chart.png')))    cells.pop(4)    cells.pop()# 在运行测试之前执行的钩子函数@pytest.hookimpl(hookwrapper=True)def pytest_runtest_makereport(item, call):    outcome = yield    report = outcome.get_result()    report.casename = str(item.function.__code__.co_varnames)

在上述示例中,首先在测试用例中生成图表并保存为 chart.png。然后,在 HTML 报告的表头中添加了一个名为 图表 的新列。在报告的行数据中,使用  标签嵌入了图表图像。

下面是配套资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

最后: 可以在公众号:程序员小濠 ! 免费领取一份216页软件测试工程师面试宝典文档资料。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。

如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值