目录

pytest-pure-report

一个美观的 pytest HTML 报告插件,具有全面的统计功能和交互特性。

✨ 功能特性

  • 🎨 精美设计:现代化的 HTML 报告,浅色主题,清晰可读
  • 🖼️ 自定义 Logo:支持本地图片或远程 URL,报告内嵌显示
  • 📝 自定义标题:支持自定义报告标题
  • 📊 统计卡片:总用例数、通过、失败、跳过、错误
  • 📈 可视化图表:进度条和饼图展示测试分布
  • 🔍 搜索过滤:支持按名称搜索和按状态筛选
  • 📋 展开收起:每个用例可展开查看详情,支持 Show/Hide all
  • ⏱️ 时间信息:测试开始时间、结束时间、总耗时
  • 🌍 环境标识:显示当前测试环境(test/dev/prod)
  • 📝 输出日志:捕获并显示测试过程中的 stdout、stderr、log
  • 📱 响应式布局:适配各种屏幕尺寸
  • ⬆️ 返回顶部:滚动时自动显示返回顶部按钮

📦 安装

# 从本地安装(开发模式)
cd pytest-pure-report
pip install -e .

# 或者从 PyPI 安装(如果已发布)
pip install pytest-pure-report

🚀 快速开始

基本使用

安装插件后,运行 pytest 时会自动生成报告:

pytest test_cases/

指定报告目录

pytest test_cases/ --report-dir=outputs/reports

指定测试环境

pytest test_cases/ --env=test

支持本地图片路径或远程 URL:

# 使用本地图片
pytest test_cases/ --logo=assets/logo.png

# 使用远程 URL
pytest test_cases/ --logo=https://example.com/logo.png

自定义标题

pytest test_cases/ --title="API 自动化测试报告"

完整示例

pytest test_cases/ \
    --report-dir=outputs/reports \
    --report-filename=test_report.html \
    --env=prod \
    --logo=assets/logo.png \
    --title="自动化测试报告" \
    -v

⚙️ 配置选项

命令行选项

选项 说明 默认值
--report-dir 报告输出目录 outputs/reports
--report-filename 报告文件名 自动生成时间戳文件名
--env 测试环境标识 test
--logo 自定义 Logo 路径(支持本地文件或 http/https URL)
--title 自定义报告标题 自动化测试报告

pytest.ini 配置

在项目根目录创建 pytest.ini 文件:

[pytest]
report_dir = outputs/reports
report_filename = test_report.html
env = test
logo = assets/logo.png
title = 自动化测试报告

环境变量配置

# 设置测试环境
export TEST_ENV=prod

# 运行测试
pytest test_cases/

配置优先级

配置项按以下优先级生效(高优先级覆盖低优先级):

  1. 命令行参数(--env, --report-dir, --logo, --title
  2. 环境变量(TEST_ENV
  3. pytest.ini 配置文件
  4. 默认值

📁 报告结构

生成的 HTML 报告包含以下部分:

┌─────────────────────────────────────────────────────────┐
│ [Logo] 自动化测试报告                                     │
├─────────────────────────────────────────────────────────┤
│ 📦 测试环境: TEST  │ 开始时间: 2024-01-01 10:00:00      │
│ 结束时间: 2024-01-01 10:01:30 │ 总耗时: 1.50m           │
├─────────────────────────────────────────────────────────┤
│ 📊 总用例数 │ ✅ 通过 │ ❌ 失败 │ ⏭️ 跳过 │ ⚠️ 错误 │
├─────────────────────────────────────────────────────────┤
│ 📈 测试进度条(通过率: 85.0%)                           │
│ 🥧 测试分布饼图                                          │
├─────────────────────────────────────────────────────────┤
│ 🔍 搜索框 | ✅ 通过 | ❌ 失败 | ⏭️ 跳过                  │
├─────────────────────────────────────────────────────────┤
│ 📋 测试结果表格                                          │
│ ├── 点击展开/收起详情                                     │
│ │   ├── 错误信息(如果有)                                │
│ │   └── 输出日志(stdout/stderr/log)                     │
│ └── Show all details / Hide all details                 │
├─────────────────────────────────────────────────────────┤
│ 🎉 报告生成完成(页脚)                                    │
└─────────────────────────────────────────────────────────┘

🧪 示例测试用例

import pytest

class TestExample:
    def test_passed(self):
        """测试通过的用例"""
        print("这是一个通过的测试")
        assert True
    
    def test_failed(self):
        """测试失败的用例"""
        print("这是一个失败的测试")
        assert False, "故意失败"
    
    @pytest.mark.skip(reason="跳过原因")
    def test_skipped(self):
        """测试跳过的用例"""
        assert True
    
    def test_with_logs(self):
        """带有日志输出的用例"""
        import logging
        logger = logging.getLogger(__name__)
        logger.info("这是一条信息日志")
        logger.warning("这是一条警告日志")
        assert True

📝 使用注意事项

  1. 不使用 -s 参数:为了捕获 print 和 log 输出,请勿使用 -s 参数,否则输出将不会被捕获
  2. 日志配置:确保 pytest 的日志捕获功能正常工作(使用 --log-cli-level 设置日志级别)
  3. 编码问题:报告文件使用 UTF-8 编码,确保测试代码中的中文输出也是 UTF-8 编码

📄 报告输出示例

运行测试后,终端会显示:

📋 pytest-pure-report v1.0.0
   测试环境: test
   报告目录: /path/to/project/outputs/reports
   Logo: assets/logo.png
   报告标题: 自动化测试报告

============================= test session starts ==============================
collected 4 items

test_cases/test_example.py::TestExample::test_passed PASSED
test_cases/test_example.py::TestExample::test_failed FAILED
test_cases/test_example.py::TestExample::test_skipped SKIPPED
test_cases/test_example.py::TestExample::test_with_logs PASSED

============================== 1 failed, 2 passed, 1 skipped ===============================

============================================================
📊 测试报告已生成:
   /path/to/project/outputs/reports/test_report_20240101_100000.html
============================================================

🔧 开发

项目结构

pytest-pure-report/
├── pyproject.toml          # 项目配置文件
├── README.md               # 项目文档
├── LICENSE                 # 许可证
├── __init__.py             # 包初始化
├── models.py               # 数据模型(TestResult)
├── generator.py            # 报告生成器(HTML 生成)
└── plugin.py               # pytest 插件(钩子和命令行选项)

本地开发

# 进入项目目录
cd pytest-pure-report

# 安装开发依赖
pip install -e .

# 运行测试
pytest test_cases/

# 构建包(可选)
python -m build

📄 许可证

MIT License

🤝 贡献

欢迎提交 Issue 和 Pull Request!


pytest-pure-report - 让测试报告更美观、更实用!

关于

一个美观的 pytest HTML 报告插件,具有全面的统计功能和交互特性。

159.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号