从0到1精通自动化测试,pytest自动化测试框架,pytest-html报告优化(十五)

112 篇文章 3 订阅
52 篇文章 6 订阅

一、前言

pytest-html测试报告默认是不展示用例描述Description内容,之前用unittest生成的报告是可以展示用例的描述,也就是test开头的用例下三个引号里面的注释(docstring)内容

pytest-html框架是可以修改生成的报告内容的,可以自己添加和删除html报告的table内容

二、修改报告

pytest-html官方文档地址【https://pypi.org/project/pytest-html/
l可以通过为标题行实现自定义钩子来修改列,下面的示例在conftest.py脚本中使用测试函数docstring添加描述(Description)列,添加可排序时间(Time)列,并删除链接(Link)列:

from datetime import datetime
from py.xml import html
import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))
    cells.insert(1, html.th('Time', class_='sortable time', col='time'))
    cells.pop()

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))
    cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
    cells.pop()

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

还可以通过pytest_html_results_table_row 挂钩删除所有单元格来删除结果。下面的示例从报表中删除所有测试通过的结果:

import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    if report.passed:
      del cells[:]

日志输出和附加HTML可以通过pytest_html_results_table_html挂钩来修改。下面的示例清空测试通过的日志输出:

import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_html(report, data):
    if report.passed:
        del data[:]
        data.append(html.div('No log output captured.', class_='empty log'))

三、添加Description

通过上面的官方文档,可以自己修改下测试报告,在报告里面添加一列的内容,添加到第二列,于是修改如下,红色代码全部注释掉
请添加图片描述
第三个@pytest.mark.hookwrapper,这个在之前测试报告里面添加截图时候,已经写过了,只需在最后加一句代码即可

report.description = str(item.function.doc)

请添加图片描述

四、代码参考

from datetime import datetime
from py.xml import html
import pytest

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    """
    当测试失败的时候,自动截图,展示到html报告中
    :param item:
    """
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_")+".png"
            screen_img = _capture_screenshot()
            if file_name:
                html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
                       'οnclick="window.open(this.src)" align="right"/></div>' % screen_img
                extra.append(pytest_html.extras.html(html))
        report.extra = extra
        report.description = str(item.function.__doc__)

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('Description'))

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))

五、效果展示

修改完之后cmd运行

pytest  —html=report.html —self-contained-html

请添加图片描述

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Python Request Pytest Pytest-HTML 集成的 Api 自动化测试框架 是一款基于 Python 编程语言的自动化测试框架,主要用于对 API 接口进行自动化测试。该框架通过使用 Python 编写测试脚本和调用 Request 库和 Pytest 框架来实现 API 测试,并且生成可视化报告,提高测试效率和可观性。 使用 Python Request Pytest Pytest-HTML 集成的 Api 自动化测试框架 进行 API 自动化测试,可以实现以下功能: 1. 自动化执行 API 接口的测试用例,提高测试效率和准确性。 2. 使用 Pytest 框架,可以灵活地编写测试脚本,支持各种测试场景和用例。 3. 使用 Request 库,可以方便地发送 HTTP 请求,并对响应结果进行处理。 4. 结合使用 Pytest-HTML,可以生成易于阅读的测试报告,包括测试结果、测试用例执行情况、错误信息等。 在使用这个框架的过程中,开发人员可以遵循一些最佳实践,例如: 1. 编写清晰明了的测试用例,并保持测试脚本的结构和格式清晰。 2. 使用参数化测试功能,以确保测试用例的完备性和可扩展性。 3. 在测试执行前,准备好测试数据和测试环境,并确保测试用例的执行顺序和前后端接口的依赖关系正确。 总之,Python Request Pytest Pytest-HTML 集成的 Api 自动化测试框架 是一款功能强大、简单易用且可扩展的自动化测试框架,适用于任何需要对 API 接口进行自动化测试的场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值