美文网首页软件测试
pytest--html报告优化

pytest--html报告优化

作者: 小啊小狼 | 来源:发表于2021-03-12 18:04 被阅读0次

一、pytest生成的原始html报告

image.png

1、在我们实际工作中,环境信息不一定要在报告中详细提现,可以增减
2、用例信息,默认展示的是用例的model名::用例名称,并不直观,所以我们可以增加一个用例描述,直观描述用例的测试内容
3、links列没有用到,可以删除
4、logs的作用往往是为了报错或者用例执行失败后调试用的,所以在用例稳定运行且成功的情况下可以去掉。
接下来我们就来针对以上4点,对我们的测试报告进行优化

二、测试报告优化

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

from py.xml import html
import pytest

1、环境信息增减

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

1.1、修改Environment项目展示的信息

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

1.2、添加Summary项目展示的信息

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

1.3、效果展示

image.png

2、增加用例描述和去除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__)

注:用例的description取值为对应用例的说明


image.png

效果展示:

image.png

3、去除执行成功用例的log输出

@pytest.mark.optionalhook
def pytest_html_results_table_html(report, data):   #清除执行成功的用例logs
    if report.passed:
        del data[:]
        data.append(html.div('自定义用例pass展示的log.', class_='empty log'))

增加以上代码输出的测试报告,如果用例执行通过,则不会展示log,会展示自己定义的输出。

image.png

相关文章

网友评论

    本文标题:pytest--html报告优化

    本文链接:https://www.haomeiwen.com/subject/vlezqltx.html