pytest之测试报告插件(pytest-html) || pytest-html测试报告的优化(测试报告标题修改、Environment修改、Summary修改) || pytest-html报告优化(添加Description测试用例描述信息列)

pytest之测试报告插件(pytest-html) 

前言

我们在测试完成之后需要查看测试用例的执行结果,pytest-html就可以帮助我们在测试完成后生成html格式的测试报告,pytest-html是一个插件,pytest可以使用pytest-html来生成html测试报告。

pytest-html插件安装

pip install pytest-html

执行命令

pytest --html=report.html

执行上述命令之后,会在当前目录下生成一个report.html的报告文件,展示效果如下:

指定报告的生成路径 

直接执行:pytest --html=report.html  命令生成的报告会在当前目录下;

如果需要指定报告的存放路径,例如报告存放在当前目录下的report文件夹下,可以执行下面的命令:

pytest --html=./report/report.html

报告独立展示

上面的方法生成的pytest-html测试报告,css是独立的;

但是如果将该pytest-html测试报告分享给他人,那么此测试报告的css样式就会丢失;

为了能更好的分享发邮件展示报告,可以将css样式合并到html中,执行以下命令:

pytest --html=report.html --self-contained-html

pytest-html官方文档

pytest-html官方文档和源码地址:https://github.com/pytest-dev/pytest-html

 

 

pytest-html测试报告的优化(报告标题修改、Environment修改、Summary修改、pytest执行失败发送邮件)

一、pytest-html标题更改

①查看原始pytest-html报告:

②修改项目根目录的 conftest.py 文件。

③通过pytest的hook功能,新添加一个 pytest_html_report_title 函数。如下:

def pytest_html_report_title(report):
    report.title = "pytest示例项目测试报告"

④重新运行测试用例,查看测试报告:可以看到此时pytest-html测试报告的标题已经修改成功。

 二、pytest-html Environment更改

①在报告中有一个Environment环境变量选项,如图所示:

②通过下图可以看到Environment显示的信息为当前项目运行的环境【metadata关键字对应的值信息】,可以修改Environment信息使其显示更贴近我们的项目。

③在 conftest.py 文件中加入以下代码。就放在更改标题代码的下方。

def pytest_configure(config):
    config._metadata.clear()
    config._metadata['测试项目'] = "测试示例项目"
    config._metadata['测试地址'] = "www.project.com"

⑤重新运行测试用例,查看测试报告:可以看到pytest-html测试报告的Environment信息已经更新。

⑥运行时可以通过控制台查看metadata关键字对应的值信息已经变成代码中设置的信息。【当然我们可以在代码中设置保留原始Environment信息并添加新的Environment信息,如三代码所示】

三、pytest-html Summary更改

①在报告中有一个Summary概要内容:表示测试套执行结果的概要信息。

②里面的内容都是与项目相关的,当然我们还可以添加一些其他的信息。在 contest.py 中添加如下内容。也放在上文函数的下方。

def pytest_html_results_summary(prefix, summary, postfix):
    # prefix.clear() # 清空summary中的内容
    prefix.extend([html.p("所属部门: XX公司测试部")])
    prefix.extend([html.p("测试执行人: 随风挥手")])

③重新运行测试用例,查看测试报告:可以看到pytest-html测试报告的Summary信息已经更新。

参考博客:https://www.cnblogs.com/wxhou/p/13167990.html

 

 

pytest-html报告优化(添加Description测试用例描述信息列)

前言

①pytest-html测试报告默认是不展示测试用例描述Description内容。

②之前用unittest测试框架生成的报告是可以展示测试用例的描述内容,也就是test开头的用例下三个引号里面的注释(docstring)内容。

③pytest-html框架是可以修改生成的测试报告内容的,可以自己添加和删除html报告的table内容。

修改pytest-html测试报告

conftest.py

# -*- encoding:utf-8 -*-
from datetime import datetime

import pytest
from py.xml import html


@pytest.fixture(scope="session")
def login():
    print("输入账号密码")
    yield
    print("清理数据完成")


def pytest_html_report_title(report):
    report.title = "pytest示例项目测试报告"


def pytest_configure(config):
    # config._metadata.clear()  # 清空原先Environment中的内容
    config._metadata['测试项目'] = "测试示例项目"
    config._metadata['测试地址'] = "www.project.com"


def pytest_html_results_summary(prefix, summary, postfix):
    # prefix.clear() # 清空summary中的内容
    prefix.extend([html.p("所属部门: XX公司测试部")])
    prefix.extend([html.p("测试执行人: 未来还未来")])


# th:表头
# td:表格
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))  # 添加描述列(Description)
    cells.insert(1, html.th('Time', class_='sortable time', col='time'))  # 添加可排序时间(Time)列
    cells.pop()  # 删除链接(Link)列


@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))
    cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
    cells.pop()


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

test1.py

# -*- encoding:utf-8 -*-
import pytest
import time


class TestLogin1:

    def test_a1(self, login):
        """
        test_a1测试用例的用例点:我爱中华大地
        """
        print("测试用例test_a1")
        time.sleep(3)
        assert 1 == 1

    def test_a2(self, login):
        """
        test_a2测试用例的用例点:我爱华夏人民
        """

        print("测试用例test_a2")

    @pytest.mark.skip("跳过test_a3")
    def test_a3(self, login):
        """
        test_a3测试用例的用例点:我爱五千年的华夏历史
        """
        print("测试用例test_a3")
        assert 1 == 1

    def test_a4(self, login):
        """
        test_a4测试用例的用例点:我爱文明中国
        """
        print("测试用例test_a4")
        assert 1 == 2


if __name__ == '__main__':
    pytest.main(['-s', '--html==./report.html'])

未实现自定义钩子函数来修改pytest-html测试报告标题行以及列:

①在conftest.py脚本中自定义钩子函数添加测试用例描述(Description)列添加可排序时间(Time)列,并删除链接(Link)列

# th:表头
# td:表格
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))  # 添加描述列(Description)
    cells.insert(1, html.th('Time', class_='sortable time', col='time'))  # 添加可排序时间(Time)列
    cells.pop()  # 删除链接(Link)列


@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))
    cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
    cells.pop()


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

运行结果:

②通过 pytest_html_results_table_row 钩子函数删除所有单元格以达到删除用例执行结果的目的。

下面的示例从pytest-html测试报告中删除所有测试通过的结果:

import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    if report.passed:
      del cells[:]

运行结果:

③日志输出和附加HTML可以通过 pytest_html_results_table_html 钩子函数来修改。

下面的示例清空测试执行结果为passed的日志输出:

import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_html(report, data):
    if report.passed:
        del data[:]
        data.append(html.div('No log output captured.', class_='empty log'))

 

参考博客:pytest文档20-pytest-html报告优化(添加Description)

posted @ 2021-07-07 21:12  习久性成  阅读(1138)  评论(0编辑  收藏  举报