pytest测试框架系列 - 一文搞定pytest-html自定义优化HTML测试报告!

前言

在我们自动化测试过程中,我们最终肯定会生成一个测试报告,测试报告可能为一个txt、xml、json文件、Excel或者HTML报告,大家基本上都偏向于HTML报告,pytest 提供了pytest-html和allure的HTML报告。本章节我们讲解使用pytest-html 插件生成HTML报告。

pytest-html用法

说明:pytest-html是pytest插件生成一个HTML测试报告。

安装

  • 当前环境:python 3.7.0
  • 前提条件:pytest (!=6.0.0,>=5.0)python > 3.0
  • 安装方式:pip install pytest-html==2.1.1 安装指定版本,3.0以上有兼容性问题,目前已经停止更新
  • 查看版本号:pip show pytest-html
    在这里插入图片描述

使用

  • 使用方式:添加命令行参数 --html=report.html--html=report.html --self-contained-html
  • 查看示例:
    test_htm.py
# !/usr/bin/python3
# _*_coding:utf-8 _*_
""""
# @Time  :2021/7/11 19:24
# @Author  : king
# @File    :test_html.py
# @Software  :PyCharm
# @blog     :https://blog.youkuaiyun.com/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
import logging

def test_01():
    logging.info("我是 test_01 测试用例")
    assert True

def test_02():
    logging.info("我是 test_02 测试用例")
    assert True

def test_03():
    logging.info("我是 test_03 测试用例")
    assert True

if __name__ == '__main__':
    pytest.main()

在命令行窗口输入: pytest --html=test_report.html test_html.py, 查看结果,在当前目录生成一个test_report.html的测试报告,在项目目录查看还有一个 assets 的样式文件目录
在这里插入图片描述
在这里插入图片描述
打开报告,可以看见一个HTML报告已经生成
在这里插入图片描述
我们删除项目目录下面的test_report.htmlassets 的样式文件目录,再次执行pytest --html=test_report.html --self-contained-html test_html.py,查看结果:只看见 test_report.html 文件,未看见样式目录
在这里插入图片描述
结论:建议使用 --html=report.html --self-contained-html方式生成报告,方便后期要通过邮件发送报告

修改pytest-html报告为中文

修改后样式:
在这里插入图片描述

修改源码位置

  • ../Lib/site-packages/pytest_html/plugin.py 文件:

469行

summary = [
            html.p(f"总共 {
     numtests} 个测试用例运行时间 {
     suite_time_delta:.2f} 秒. "),
            html.p(
                "可以通过选择筛选项筛选测试结果。",
                class_="filter",
                hidden="true",
            ),
        
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试之路king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值