【pytest-html】深度探索pytest-html测试报告的自定义使用

本文详细介绍了如何深度定制pytest-html的报告样式,包括修改报告标题、环境配置、增加额外内容和调整结果表结构。通过实例演示了如何使用钩子函数实现动态测试环境的显示,并解决了错误日志捕捉的问题。

一、前言

插件项目地址:https://github.com/pytest-dev/pytest-html

官方文档地址:https://pytest-html.readthedocs.io/en/latest/

二、安装

依赖

  • Python >=3.6
  • or PyPy3

直接安装

pip install pytest-html

从源码安装

 pip install -e .

三、基本使用

# 直接在当前目录生成名称为report.html的报告,同时会自动生成一个assets样式文件目录
pytest --html=report.html
# 直接在当前目录生成名称为report.html的报告,报告自带样式
pytest --html=report.html --self-contained-html

四、深度使用

1. 改变报告样式

pytest --html=report.html --css=自定义的样式文件的路径
"""
注意:自定义的样式文件不要写中文,否则会报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 33: illegal multibyte sequence
尝试了在css文件首行增加:@charset "UTF-8";  但是没有解决这个问题。
"""

示例

  1. 首先新建一个css文件,入如:report.css
    在这里插入图片描述

  2. 打开pytest-html生成的报告,用F12查看标签的相关信息,class,id,标签名等

在这里插入图片描述
3. 先尝试直接通过F12更改报告样式,看是否有效。(当前如果你很会css,可以忽略这个步骤)
在这里插入图片描述

  1. 拷贝样式代码到report.css
    在这里插入图片描述

  2. 运行测试时,指定该css文件:pytest --html=report.html --css=report.css
    在这里插入图片描述

  3. 再次打开报告,可以看到我们指定的css样式生效了。
    在这里插入图片描述

2. 修改报告标题

默认情况下,pytest-html会使用报告文件名作为报名标题,这里我们可以通过钩子函数pytest_html_report_title更改报告标题。

def pytest_html_report_title(report):
    report.title = "My very own title!"

示例

  1. 找到项目根目录下的conftest.py文件,往里面粘贴上面的代码,其中标题是可以自定义的。
    在这里插入图片描述

  2. 重新生成报告就可以看到效果了。
    在这里插入图片描述

3. 修改Enviroment

环境模块是由 pytest-metadata 插件提供的。有两个钩子函数:pytest_configurepytest_sessionfinish

在测试运行前修改环境配置pytest_configure

def pytest_configure(config):
    config._metadata["键"] = "值"

在测试运行后修改环境配置pytest_sessionfinish

import pytest

@pytest.hookimpl(tryfirst=True)
def pytest_sessionfinish(session, exitstatus):
    session.config._metadata["键"] = "值"

注意:

  • @pytest.hookimpl(tryfirst=True)非常重要;
  • 它可以使得pytest_sessionfinish在任何其他插件(包括pytest-htmlpytest-metadata)运行前运行;
  • 如果我们没有增加@pytest.hookimpl(tryfirst=True),就会导致环境表不会按照我们预想的发生变化。

示例

  1. 在conftest.py文件上增加如下代码
    在这里插入图片描述

  2. 重新运行生成报告后,报告如下图所示
    在这里插入图片描述

问题

官方文档这部分功能,没有理解,大家如果搞定了可以给我留言,谢谢啦!
在这里插入图片描述

4. 修改Summary

我们可以通过钩子函数pytest_html_results_summary修改summary部分内容。

from py.xml import html


def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.p("追加的内容")])

示例

  1. 在conftest.py文件上增加如下代码
    在这里插入图片描述

  2. 重新运行生成报告后,报告如下图所示
    在这里插入图片描述

5. 增加额外的内容

我们能通过extra给报告增加更多的详细信息。以下是我们可以增加的内容。

Type Example
Raw HTML extra.html(‘
Additional HTML
’)
JSON extra.json({‘name’: ‘pytest’})
Plain text extra.text(‘Add some simple Text’)
URL extra.url(‘http://www.example.com/’)
Image extra.image(image, mime_type=‘image/gif’, extension=‘gif’)
Image extra.image(‘/path/to/file.png’)
Image extra.image(‘http://some_image.png’)

注意:

  • 当我们从文件中增加一个图片时,图片的路径可以是绝对路径或者相对路径。

  • 当我们使用 --self-contained-html, 图片也许无法按照预期的加载出来。(这里我试过,是可以正常加载的。

  • 官方文档说图片还可以使用如下格式:

Image format Example
PNG extra.png(image)
JPEG extra.jpg(image)
SVG extra.svg(image)

但是我亲自尝试过,直接使用extras.image就可以加载上述类型的图片。上述指定格式显得稍有点多余。

而且注意:官方文档都是写的extra.image 或者 extra.svg, extra没有带s,实际示例中有s,实际使用也需要用extras。这里不知道是不是我理解的有问题,还是官方文档笔误,大家注意一下。

我们可以在conftest.py中通过钩子函数pytest_runtest_makereport 来增加这些额外的内容。

import pytest


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin("html")
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, "extra", [])
    if report.when == "call":
        # always add url to report
        extra.append(pytest_html.extras.url("http://www.example.com/"))
        xfail = hasattr(report, "wasxfail")
        if (report.skipped and xfail) or (report.failed and not xfail):
            # only add additional html on failure
            extra.append(pytest_html.extras.html("<div>Additional HTML</div>"))
        report.extra = extra

示例1

  1. 首先修改我们的用例,让用例产生各种类型的数据,方便看效果。
    在这里插入图片描述

  2. 我们在conftest.py文件中增加如下代码

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin("html")
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, "extra", [])
    if report.when == "call":
        # 增加URL
        extra.append(pytest_html.extras.url("https://www.gitlink.org.cn"))
        # 增加json
        extra.append(pytest_html.extras.json({
   
   "name": "pytest"}))
        # 增加plain text
        extra.append(pytest_html.extras.text("这里是xxx自动化测试用例"))
        xfail = hasattr(report, "wasxfail")
        if (report.skipped and xfail) or (report
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值