如何向报告中添加附加信息
在使用 pytest-html
生成的报告中,您可以通过创建一个 extra
列表并将其添加到报告对象中,向报告中添加额外的详细信息。这些附加信息可以是文本、图片、链接等多种类型。以下是几种常用的附加信息类型及其添加方式:
常见附加信息类型及示例
信息类型 | 示例 |
---|---|
原始 HTML | extras.html('<div>Additional HTML</div>') |
JSON 格式 | extras.json({'name': 'pytest'}) |
纯文本 | extras.text('Add some simple Text') |
链接 (URL) | extras.url('http://www.example.com/') |
GIF 动图 | extras.image(image, mime_type='image/gif', extension='gif') |
链接 (URL) | extras.url('http://www.example.com/', name="点击查看示例") |
PNG 图片 | extras.png(base64_encoded_string, name = '图片名称') |
JPG 图片 | extras.jpg(base64_encoded_string, name = '图片名称') |
SVG 图片 | extras.svg(base64_encoded_string, name = '图片名称') |
通用图片 | extras.image(base64_encoded_string, name = '图片名称') |
本地图片 | extras.image('/path/to/file.png') |
网络图片 | extras.image('http://some_image.png') |
注意事项:
- 当您添加图片时,路径既可以是绝对路径,也可以是相对路径。
- 如果您使用了
--self-contained-html
选项,将图片文件或链接形式添加到报告中时,图片可能无法正确显示。详情请查看创建自包含报告。
使用 pytest hook 添加附加信息
您可以通过 pytest_runtest_makereport
钩子函数,在测试执行过程中为报告添加自定义的附加信息。这通常在 conftest.py
或插件中实现。下面是一个示例,展示了如何添加 URL、HTML 等不同类型的附加信息:
Python
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":
# 始终添加一个 URL 到报告中
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):
# 仅在测试失败时添加额外的 HTML
extra.append(pytest_html.extras.html("<div>Additional HTML</div>"))
report.extra = extra
在测试函数中直接添加信息
如果您不想实现钩子函数,可以使用 extra
fixture 在测试函数内部直接添加附加信息。这些内容通常会显示在钩子添加的附加信息之前。
Python
from pytest_html import extras
def test_extra(extra):
# 添加文本信息
extra.append(extras.text("some string"))
# 添加截图到测试报告中
extra.append(extras.image(model.getWindow("Dialogs").takeScreenshot()))
为附加信息指定自定义标题
除了 html
类型外,您可以为其他类型的附加信息指定一个 name
参数,这样就可以自定义该信息在报告中的显示标题。例如:
Python
extra.append(pytest_html.extras.text("some string", name="自定义标题"))
使用 Hook 在测试失败时自动截图
推荐使用 pytest_runtest_makereport
钩子函数,在测试执行和报告生成过程中动态添加信息。这是一个非常实用的例子,它展示了如何在测试用例失败时自动进行截图并将其添加到报告中。
Python
# conftest.py
import pytest
from pytest_html import extras
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""
每个测试用例执行后生成报告的 hook 函数。
"""
outcome = yield
report = outcome.get_result()
# report.when 只在 "call" 阶段考虑,以避免在 setup 和 teardown 中重复添加
if report.when == "call":
# extras 列表用于存储要添加到报告中的附加信息
extra = getattr(report, "extras", [])
# 判断测试用例是否失败
if report.failed:
# 这里添加您的截图逻辑,例如使用 Selenium 或其他 UI 测试库
# screenshot_base64 = model.getWindow("Window").takeScreenshot()
screenshot_base64 = model.getWindow("Standard_Dialogs").takeScreenshot()
# 使用 extras.image 添加图片,并用 name 参数自定义标题
extra.append(extras.image(screenshot_base64, name="失败截图"))
report.extras = extra
报告的全局自定义
pytest-html
v4+ 版本引入了新的 hook 函数,让您可以轻松自定义报告的全局属性。
1. 自定义报告标题
使用 pytest_html_report_title
钩子函数来修改报告的 <title>
和主标题。
Python
# conftest.py
def pytest_html_report_title(report):
"""
修改报告的标题。
"""
report.title = "接口自动化测试报告"
2. 自定义 Duration
显示格式
使用 pytest_html_duration_format
钩子函数,您可以修改测试用时(Duration)的显示格式,例如添加单位。
Python
# conftest.py
def pytest_html_duration_format(duration: float) -> str:
"""
自定义 duration 的显示格式,保留两位小数并添加 "秒" 作为单位。
"""
return f"{duration:.2f} 秒"