pytest教程-9-pytest-html生成html报告

领取资料,咨询答疑,请➕wei:  June__Go

上一小节我们学习了pytest用例参数化方法,本小节我们讲解一下使用pytest-html生成html测试报告。

自动化测试执行完成后我们需要展示给其他人看,这就要有自动化测试报告了。复杂的测试报告当然可以自己代码实现,但用pytest-html或allure基本也能满足我们生成测试报告的要求了。本小节介绍pytest-html的使用。

1、pytest-html安装

pytest-html属于pytest的一个插件,使用它需要先安装,执行如下命令

pip install pytest-html

成功安装

pytest教程-9-pytest-html生成html报告_第1张图片


pytest其实可以生成多种样式的结果:

生成JunitXML格式的测试报告,命令:--junitxml=path

生成ResultLog格式的测试报告,命令:--resultlog=report/log.txt

生成Html格式的测试报告,命令:--html=OutPuts/reports/report.html(相对路径)

2、生成html报告

先准备一个简单的执行脚本

import pytest


def fun(x):
    return x + 1


def test_answer_1():
    """测试断言一"""
    assert fun(3) == 4


def test_answer_2():
    """测试断言二"""
    assert fun(5) == 7


@pytest.mark.parametrize("test_input,expected", [
    ("3+5", 8),
    ("2+4", 6),
    pytest.param("6 * 9", 42, marks=pytest.mark.xfail),
    pytest.param("6 * 6", 42, marks=pytest.mark.skip)
])
def test_mark(test_input, expected):
    """用例集合"""
    assert eval(test_input) == expected

2.1在当前目录下生成测试报告,执行命令

pytest --html=报告名称 要执行的脚本文件
示例: pytest --html=report.html .\test_demo.py

当前目录下会生成report.html报告

pytest教程-9-pytest-html生成html报告_第2张图片

使用chrome浏览器打开测试报告

pytest教程-9-pytest-html生成html报告_第3张图片

pytest教程-9-pytest-html生成html报告_第4张图片

2.2在指定路径生成测试报告,执行命令

pytest --html=指定的报告路径 要执行的脚本文件
示例: pytest --html=.\myreport\report.html .\test_demo.py

当前myreport目录下会生成html报告

pytest教程-9-pytest-html生成html报告_第5张图片

补充说明:生成测试报告的时候,如果在目标路径下已经存在同名的报告,原报告会被覆盖掉。

2.3创建一个独立的报告

先我们来看下上面的方法生成的测试报告,css和html是分开存储的。分享报告的时候css样式会丢失。

pytest教程-9-pytest-html生成html报告_第6张图片

可以把css样式合并到html里,建议使用下面的方法来创建一个独立的报告。

pytest --html=report.html --self-contained-html .\test_demo.py

独立报告已生成

pytest教程-9-pytest-html生成html报告_第7张图片

3、定制化html测试报告

观察上面的测试报告可知,默认生成的测试报告有Title、Environment、Summary、Results 加上表格,五个部分。pytest-html 插件提供了hooks钩子函数来帮助我们实现定制化报告,钩子函数需要写入conftest.py文件。

conftest.py文件如下:高版本的pytest需要提前安装pytest-metadata,安装命令为:pip install pytest-metadata

# conftest.py
import pytest
from py._xmlgen import html
from datetime import datetime


# 1、修改报告标题
def pytest_html_report_title(report):
    report.title = "我的测试报告标题"


# 2、运行测试前修改环境信息
@pytest.hookimpl(optionalhook=True)
def pytest_metadata(metadata: dict):
    metadata['项目名称'] = '我的项目'
    metadata['接口地址'] = "https://www.example.com"


# 3、修改摘要信息
def pytest_html_results_summary(prefix, summary, postfix):
    prefix.extend([html.p("所属部门: 测试保障部")])
    prefix.extend([html.p("测试人员: 张三")])


# 4、测试结果表格
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th("Description"))  # 表头添加Description
    cells.insert(2, html.th("Time", class_="sortable time", col="time"))
    cells.pop(-1)  # 删除link


@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))  # 表头对应的内容
    cells.insert(2, html.td(datetime.now(), class_="col-time"))
    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__)
    report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")

修改完成,重新执行脚本,查看最终效果。

pytest教程-9-pytest-html生成html报告_第8张图片

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走,希望可以帮助到大家!领取资料,咨询答疑,请➕wei:  June__Go

你可能感兴趣的:(pytest教程,pytest)