28-pytest-生成HTML报告


前言

  • 本篇来学习使用pytest-html插件生成HTML报告

安装包

pip install pytest-html

使用示例

  • –html=report.html : css 文件和html报告是分开的
# -*- coding: utf-8 -*-
# @Time    : 2022/3/21
# @Author  : 大海

import os


def test_add():
    a = 1 + 2
    assert a == 3


if __name__ == '__main__':
    # --html=报告名.html   css和html 是分开的
    os.system('pytest -s test_52.py --html=report.html')
  • –self-contained-html : 合并css 到html报告中
# -*- coding: utf-8 -*-
# @Time    : 2022/3/21
# @Author  : 大海

import os


def test_add():
    a = 1 + 2
    assert a == 3


if __name__ == '__main__':
    # --self-contained-html  css样式合并到html中
    os.system('pytest -s test_52.py --html=report.html --self-contained-html')
  • 查看HTML报告
    在这里插入图片描述

修改HTML报告

  • 添加描述(Description)列,添加可排序时间(Time)列,并删除链接(Link)列
  • 新建conftest.py文件(文件名固定,不要改),代码如下
# -*- coding: utf-8 -*-
# @Time    : 2022/3/21
# @Author  : 大海

from datetime import datetime
from py.xml import html
import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))
    cells.insert(-1, html.th('Time', class_='sortable time', col='time'))
    cells.pop()

@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__)
  • 测试方法文件 test_53.py
# -*- coding: utf-8 -*-
# @Time    : 2022/3/21
# @Author  : 大海

import os
import time


def test_add():
    """验证1+2等于3"""
    a = 1 + 2
    time.sleep(2)
    assert a == 3


if __name__ == '__main__':
    os.system('pytest -s test_53.py --html=./report/report.html --self-contained-html')
  • 查看HTML报告
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱学习de测试小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值