【Pytest】使用Allure生成企业级测试报告

目录

  • [1 Allure测试报告框架](#1 Allure测试报告框架)
  • [2 Allure核心装饰器](#2 Allure核心装饰器)
    • [2.1 @allure.epic](#2.1 @allure.epic)
    • [2.2 @allure.feature](#2.2 @allure.feature)
    • [2.3 @allure.story](#2.3 @allure.story)
    • [2.4 @allure.title](#2.4 @allure.title)
    • [2.5 @allure.severity](#2.5 @allure.severity)
    • [2.6 @allure.step](#2.6 @allure.step)
    • [2.7 @allure.attach](#2.7 @allure.attach)
  • [3 演示:使用Allure实现测试用例分组](#3 演示:使用Allure实现测试用例分组)
  • 写在最后

1 Allure测试报告框架

Allure 是一个轻量级、多语言支持的自动化测试报告框架,最初由 Yandex 团队开发,后成为开源项目。它通过清晰的测试步骤分层、丰富的注解支持和直观的可视化报告界面,解决了测试结果难以追溯与沟通的痛点,成为众多测试团队的首选报告工具。

安装:

shell 复制代码
pip install allure-pytest

配置(命令行或pytest.ini):

shell 复制代码
--alluredir=temps --clean-alluredir

生成Allure测试报告:

shell 复制代码
allure generate -o report -c temps

示例代码:

python 复制代码
import pytest
import os

pytest.main()

# 生成Allure测试报告
os.system("allure generate -o report -c temps")

注,使用allure前需要确保已经安装了Allure 命令行工具,以及依赖的Java环境

shell 复制代码
wget https://github.com/allure-framework/allure2/releases/download/2.12.1/allure-commandline-2.12.1.zip
unzip allure-commandline-2.12.1.zip -d D:\develop\allure

也可以参考博客:allure的安装和使用(windows环境),进行手动安装

配置完成后,运行pytest,可以看到在temps目录下生成了报告的元数据,而后,我们通过allure命令生成最终的测试报告:

2 Allure核心装饰器

Allure 提供多种装饰器用于增强测试报告的可读性与结构化。总体层级遵循 :

● Epic > Feature > Story > Title

2.1 @allure.epic

定义测试用例的史诗(Epic),通常用于表示项目需求或大模块。在 Behaviors 栏目下按层级分组展示,便于按需求模块管理测试用例。

示例代码:

python 复制代码
@allure.epic('需求1')
@allure.feature('功能模块1')
@allure.story('子功能1')
@allure.title('用例1')
def test_case1():
    pass

2.2 @allure.feature

定义功能模块(Feature),是 Epic 的子集。在 Behaviors 栏目下按 Epic 分组,再按 Feature 展示用例。

示例代码:

python 复制代码
@allure.feature('用户登录功能')
def test_login():
    pass

2.3 @allure.story

定义子功能或场景(Story),是 Feature 的子集。在 Behaviors 栏目下按 Epic → Feature → Story 层级展示。

示例代码:

python 复制代码
@allure.story('登录成功场景')
def test_login_success():
    pass

2.4 @allure.title

自定义测试用例标题,覆盖默认名称。

示例代码:

python 复制代码
@allure.title('验证登录失败时的错误提示')
def test_login_failure():
    pass

2.5 @allure.severity

定义测试用例的严重级别(如 blocker、critical、normal),用于优先级排序。

示例代码:

python 复制代码
@allure.severity(allure.severity_level.CRITICAL)
def test_critical_functionality():
    pass

2.6 @allure.step

将测试步骤分层显示,便于追踪执行过程。

示例代码:

python 复制代码
@allure.step('执行登录操作')
def login(username, password):
    pass

2.7 @allure.attach

附加额外信息(如日志、截图)到测试用例中。

示例代码:

python 复制代码
@allure.attach('登录请求数据', '{"username":"admin", "password":"123"}', allure.attachment_type.JSON)
def test_login():
    pass

3 演示:使用Allure实现测试用例分组

编写三个测试用例,其中两个属于同一个Epic,不同的story,不同的feature;一个用例不属于任何Epic。

示例代码:

python 复制代码
import allure
import pytest


@allure.epic('测试开发自动化测试')
@allure.feature('Pytest自动化测试框架')
@allure.story('mark标记')
@allure.title('mark测试用例')
@pytest.mark.al
def test_allure_a():
    assert True


@allure.epic('测试开发自动化测试')
@allure.feature('Pytest自动化测试框架')
@allure.story('Allure框架生成测试报告')
@allure.title('allure测试用例')
@pytest.mark.al
def test_allure_b():
    assert True


@pytest.mark.al
def test_allure_c():
    assert True

演示结果:


写在最后

本文已被专栏 测试开发知识库 收录,欢迎 点击订阅专栏

以上便是本文的全部内容啦!创作不易,如果你有任何问题,欢迎私信,感谢您的支持!

相关推荐
wj3055853788 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李8 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154159 小时前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君12 小时前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG13 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户83562907805113 小时前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL14 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
猫猫的小茶馆14 小时前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32
Miss_min15 小时前
128K长序列数据生成
开发语言·python·深度学习