pytest文档20-pytest-html报告优化

前言

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;" '                       'onclick="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

(0)

相关推荐

  • Jenkins构建后展示Allure测试报告

    上一篇介绍了通过Pytest生成Allure测试报告,这篇主要介绍Ubuntu通过Jenkins构建Python Pytest Requests Allure自动化项目后展示Allure测试报告 环境 ...

  • pytest文档7-生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  • pytest文档13-allure2生成html报告(史上最详细)

    前言 allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. 环境准备 1. ...

  • pytest文档67-在 pytest.mark.parametrize 中使用 fixture

    前言 测试用例参数化的时候,使用 pytest.mark.parametrize 参数化传测试数据,如果我们想引用前面 不同fixture 返回的数据当测试用例的入参,目前没好的解决办法. 可以用fi ...

  • pytest文档21-pytest-html报告优化

    前言 pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode ...

  • pytest文档8-html报告报错截图+失败重跑

    前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...

  • pytest文档39-参数化(parametrize)结合allure.title()生成不同标题报告

    前言 pytest的参数化(parametrize)可以实现只需维护测试数据,就能生成不同的测试用例目的.可以在参数化的时候加 ids 参数对每个用例说明使用场景. 最终我们希望在 allure 报告 ...

  • pytest文档47-allure报告添加用例失败截图

    前言 使用 selenium 做 web 自动化的时候,很多小伙伴希望用例失败的时候能截图,把异常截图展示到allure报告里面. pytest 有个很好的钩子函数 pytest_runtest_ma ...

  • pytest文档1-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  • pytest文档2-用例运行规则

    用例设计原则 文件名以test_*.py文件和*_test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 help帮 ...