Pytest-html报告优化+增加错误截图,获取统计数据

1、在我们实际工作中,环境信息不一定要在报告中详细提现,可以增减

2、用例信息,默认展示的是用例的model名::用例名称,并不直观,所以我们可以增加一个用例描述,直观描述用例的测试内容

3、links列没有用到,可以删除

4、logs的作用往往是为了报错或者用例执行失败后调试用的,所以在用例稳定运行且成功的情况下可以去掉。
接下来我们就来针对以上4点,对我们的测试报告进行优化

测试报告优化

测试报告格式优化,所有内容均需要在conftest.py中配置
首先引入以下包

from py.xml import html
import pytest

环境信息增减

conftest.py 中增加以下代码,具体需要增加和去除的信息根据个人需要调整。

修改Environment项目展示的信息

def pytest_configure(config):
    # 添加项目名称
    config._metadata["项目名称"] = "钣喷车间小程序测试"
    # 删除Java_Home
    config._metadata.pop("JAVA_HOME")
    # 删除Plugins
    config._metadata.pop("Plugins")

添加Summary项目展示的信息

@pytest.mark.optionalhook
def pytest_html_results_summary(prefix):  #添加summary内容
    prefix.extend([html.p("所属部门: test")])
    prefix.extend([html.p("测试人员: jack")])

增加用例描述和去除link列

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('Description'))  # 表头添加Description
    cells.pop(-1)  # 删除link


@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))  #表头对应的内容
    cells.pop(-1)  # 删除link列


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):  #description取值为用例说明__doc__
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
posted @ 2023-02-15 14:49  郭小睿  阅读(167)  评论(0编辑  收藏  举报