更新
一个美观的 pytest HTML 报告插件,具有全面的统计功能和交互特性。
# 从本地安装(开发模式) 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
--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/
配置项按以下优先级生效(高优先级覆盖低优先级):
TEST_ENV
生成的 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
-s
--log-cli-level
运行测试后,终端会显示:
📋 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 - 让测试报告更美观、更实用!
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802047560号
pytest-pure-report
一个美观的 pytest HTML 报告插件,具有全面的统计功能和交互特性。
✨ 功能特性
📦 安装
🚀 快速开始
基本使用
安装插件后,运行 pytest 时会自动生成报告:
指定报告目录
指定测试环境
自定义 Logo
支持本地图片路径或远程 URL:
自定义标题
完整示例
⚙️ 配置选项
命令行选项
--report-diroutputs/reports--report-filename--envtest--logo--titlepytest.ini 配置
在项目根目录创建
pytest.ini文件:环境变量配置
配置优先级
配置项按以下优先级生效(高优先级覆盖低优先级):
--env,--report-dir,--logo,--title)TEST_ENV)📁 报告结构
生成的 HTML 报告包含以下部分:
🧪 示例测试用例
📝 使用注意事项
-s参数,否则输出将不会被捕获--log-cli-level设置日志级别)📄 报告输出示例
运行测试后,终端会显示:
🔧 开发
项目结构
本地开发
📄 许可证
MIT License
🤝 贡献
欢迎提交 Issue 和 Pull Request!
pytest-pure-report - 让测试报告更美观、更实用!