当前位置: 首页 > news >正文

pytest 测试报告生成方案有哪些?

在 pytest 中,除了 Allure 和 HTMLTestRunner,还有许多其他生成测试报告的方法和插件。以下是一些常用的方案及其特点:

1. pytest-html(官方推荐)

特点:轻量级、易集成,生成独立的 HTML 报告。

安装

pip install pytest-html

使用

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

示例报告

2. pytest-cov(代码覆盖率报告)

特点:统计测试覆盖率,支持 HTML、XML 等格式。

安装

pip install pytest-cov

使用

pytest --cov=your_project --cov-report html

示例报告

3. pytest-xdist(分布式测试报告)

特点:并行执行测试,生成汇总报告。

安装

pip install pytest-xdist

使用

pytest -n auto --html=report.html  # 自动检测 CPU 核心数并行执行

4. pytest-reportlog(JSON 格式报告)

特点生成结构化的 JSON 报告,便于后续处理

使用

pytest --reportlog=report.json

5. pytest-sugar(美化控制台输出)

特点:美化测试执行过程的控制台输出,不生成文件报告。

安装

pip install pytest-sugar

示例输出

6. pytest-rerunfailures(失败重试报告)

特点:自动重试失败的测试用例,并在报告中标记。

安装

pip install pytest-rerunfailures

使用

pytest --reruns 3 --html=report.html  # 失败重试 3 次

7. pytest-bdd(行为驱动开发报告)

特点:基于 Gherkin 语法,生成 BDD 风格的测试报告。

安装

pip install pytest-bdd

示例测试用例

# features/login.feature

Scenario: 登录成功

  Given 用户已注册

  When 用户输入正确的用户名和密码

  Then 登录成功

8. pytest-json-report(JSON 报告)

特点:生成详细的 JSON 格式报告。

安装

pip install pytest-json-report

使用

pytest --json-report --json-report-file=report.json

9. pytest-testmon(增量测试报告)

特点:只运行变更的测试,生成增量报告。

安装

pip install pytest-testmon

使用

pytest --testmon  # 首次运行会记录状态

pytest --testmon  # 后续只运行变更的测试

10. 自定义插件

特点:根据需求开发自定义报告插件。

示例代码

# conftest.py

import pytest

@pytest.hookimpl(tryfirst=True, hookwrapper=True)

def pytest_runtest_makereport(item, call):

    outcome = yield

    report = outcome.get_result()

    

    if report.when == 'call':

        print(f"测试 {item.nodeid} 结果: {report.outcome}")

对比与选择建议

插件

报告格式

特点

适用场景

pytest-html

HTML

简单易用,适合基础报告

日常测试

allure-pytest

HTML

功能丰富,支持步骤、附件

正式项目、对外展示

pytest-cov

HTML/XML

代码覆盖率统计

质量保障、合规要求

pytest-xdist

汇总报告

并行测试

大型项目、性能优化

pytest-bdd

BDD 风格

业务与技术对齐

敏捷开发、需求沟通

组合使用示例

同时生成 HTML 报告和覆盖率报告:

pytest --html=report.html --cov=your_project --cov-report html

根据项目需求,你可以选择单一插件或组合使用多种插件来满足不同的报告需求。

http://www.dtcms.com/a/297617.html

相关文章:

  • Springboot整合springmvc
  • 基于Docker的GPU版本飞桨PaddleOCR部署深度指南(国内镜像)2025年7月底测试好用:从理论到实践的完整技术方案
  • 【赵渝强老师】MySQL中的数据库对象
  • 7月25号打卡
  • 海康监控集中管理解决方案全面分析
  • 设计仿真 | Simufact Forming模具疲劳分析助力预测模具寿命
  • 基于单片机的楼宇门禁系统的设计与实现
  • 目标导向的强化学习:问题定义与 HER 算法详解—强化学习(19)
  • [特殊字符] GitHub 2025年7月月度精选项目 Top5
  • Java NIO FileChannel在大文件传输中的性能优化实践指南
  • Movavi Video Editor v25.9.0 视频编辑软件中文特别版
  • Pytorch中cuda相关操作详见和代码示例
  • 【Chrome】下载chromedriver的地址
  • 线性代数 下
  • Chrome(谷歌)浏览器 数据JSON格式美化
  • JAVA知识点(六):性能调优与线上问题排查
  • vue+iview+i18n国际化
  • Day 3: 机器学习进阶算法与集成学习
  • 《Uniapp-Vue 3-TS 实战开发》自定义环形进度条组件
  • Zookeeper 3.6.3【详细技术讲解】整
  • Uniapp编写微信小程序,绘制动态圆环进度条
  • Welcome to the world of Go language
  • 鸿蒙端云一体化开发之创建和操作数据库
  • 内存 管理
  • 重读《人件》Peopleware -(22)Ⅲ 适当人选 Ⅵ 乐在其中(上)
  • 微服务架构中的资源调度与负载均衡实践
  • 股指期权可以随时平仓吗?
  • OSPF之多区域
  • cha的操作
  • 每日面试题14:CMS与G1垃圾回收器的区别