pytest结合allure

news2025/4/22 2:23:26

Allure

  • 一、文档
  • 二、指令
  • 三、装饰器
    • 3.1 @allure.step装饰器
    • 3.2 @allure.description装饰器
    • 3.3 @allure.title装饰器
    • 3.4 @allure.link、@allure.issue 和 @allure.testcase装饰器
    • 3.5 @allure.epic、@allure.feature 和 @allure.story装饰器
    • 3.6 @allure.severity装饰器

一、文档

allure文档

二、指令

1、运行测试并生成 allure 数据

pytest --alluredir=./allure_results

2、生成并打开报告

allure serve ./allure_results

三、装饰器

3.1 @allure.step装饰器

将函数或方法标记为测试步骤,并在 Allure 报告中展示步骤层级。
直接在测试方法或函数上添加 @allure.step 装饰器,并可自定义步骤名称,支持使用{}占位符。

import allure

@allure.step("打开应用首页")
def open_homepage():
    print("-----------open--------------")
    pass

# 使用 {} 占位符将变量嵌入步骤名称,参数自动填充。
@allure.step("输入用户名和密码: {username},{password}")
def input_username(username, password):
    print(f"-----------{username}--------------")
    pass

# 步骤可以嵌套,形成清晰的逻辑层级
def test_login():
    open_homepage()
    input_username("test_user","1234")

conftest.py与@allure.step结合,显示单独的“前置/后置”树,用于配置初始化和销毁。
conftest.py:

import pytest
import allure

@allure.step("step in conftest.py for setup")
def conftest_test():
    pass

@allure.step("step in conftest.py for teardown")
def conftest_test_of_teardown():
    pass

@pytest.fixture(autouse=True)
def fixture_with_conftest_test():
    conftest_test()
    yield
    conftest_test_of_teardown()

运行结果:
在这里插入图片描述

3.2 @allure.description装饰器

为测试用例添加详细描述

1、@allure.description提供描述字符串
2、@allure.description_html添加html格式的描述
3、仅从测试方法的文档字符串获取描述

import pytest
import allure

def test_unicode_in_description():
    """
    unicode描述使用不同的国家语言
    hello
    こんにちは
    你好伙计
    """
    assert 42 == int(6 * 7)


@pytest.mark.parametrize("username", ["user1", "user2"])
@allure.description("测试不同用户名的登录兼容性:username={username}")
def test_login_compatibility(username):
    pass

@allure.description_html("""
<h1>添加html格式的描述</h1>
""")
def test_description_html():
    pass


@allure.description("动态描述,可替换开始的描述")
def test_login_change():
    pass
    allure.dynamic.description("测试用例执行完了,更改描述")

在这里插入图片描述

3.3 @allure.title装饰器

使测试标题更具可读性,标题支持占位符并支持动态替换

import pytest
import allure

@allure.title("@allure.title使测试标题更具可读性,标题支持占位符并支持动态替换")
def test_with_a_title():
    assert 2 + 2 == 4


@pytest.mark.parametrize("param1,param2,expected", [(1, 1, 2), (1, 3, 5)])
@allure.title("标题包含动态参数:{param1},{param2}")
def test_with_parametrize_title(param1, param2, expected):
    assert param1 + param2 == expected

@allure.title("动态描述,可替换开始的描述")
def test_title_update():
    pass
    allure.dynamic.title("测试用例执行完了,更改标题")

运行结果:
在这里插入图片描述

3.4 @allure.link、@allure.issue 和 @allure.testcase装饰器

用于在Allure报告中添加可点击的链接,提升测试报告的可追溯性。其中@allure.link表示通用资源链接,@allure.issue关联问题追踪系统(Bug/任务),@allure.testcase关联测试用例管理系统

@allure.link("https://github.com/youngyangyang04/leetcode-master", name="项目仓库")
def test_example_for_link():
    pass

@allure.issue("PROJECT-123", "登录失败问题")
def test_login_for_issue():
    # 假设配置了issue链接模板:https://jira.company.com/browse/{issue}
    # 报告中将自动生成完整链接。
    pass

@allure.testcase("TC-456", "登录功能验证")
def test_login_for_testcase():
    # 假设配置了testcase链接模板:https://testrail.company.com/index.php?/cases/view/{testcase}
    pass

运行结果:
在这里插入图片描述

3.5 @allure.epic、@allure.feature 和 @allure.story装饰器

Epic、Feature、Story通常用于行为驱动开发(BDD)中的分层结构。Epic是最大的范围,通常对应大的业务目标;Feature是Epic下面的功能模块;Story则是更小的用户故事或功能点。Tag可能更灵活,用于标记测试的其他属性,比如优先级、模块等。

注解层级定位典型用途
@allure.epic最高层级,表示宏观业务目标或大型项目模块。描述系统级别的业务需求或长期目标。
@allure.feature中间层级,属于某个epic下的功能模块。描述一个独立的功能模块或子系统。
@allure.story最细层级,属于某个feature下的具体用户故事或功能点。描述具体的用户场景或需求点(通常对应敏捷开发中的User Story)。
@allure.tag独立标签,无固定层级,灵活标记任意属性。补充标记测试的附加属性(如优先级、测试类型、模块等)。
import allure

@allure.epic("电商平台")
@allure.feature("订单管理")
@allure.story("用户取消订单")
@allure.tag("smoke", "high-priority")
def test_order_cancellation():
    pass

@allure.feature("feature_1")
def test_example_for_feature():
    pass


@allure.story("story_1")
def test_example_for_story():
    pass

@allure.feature("feature_2")
@allure.story("story_1")
def test_example_mix_feature_and_story():
    pass


@allure.feature("feature_2")
@allure.story("story_2")
def test_example_mix_feature_and_story_2():
    pass

可以使用如下命令行选项指定不同的测试集(可有多个,以逗号分隔

--allure-epics xx1,xx2
--allure-features xx1,xx2
--allure-stories xx1,xx2

如:

pytest --alluredir=./allure_results --allure-epics "电商平台"
pytest --alluredir=./allure_results --allure-stories story_1,story_2
pytest --alluredir=./allure_results --allure-features feature_2 --allure-stories story_2 #--allure-features 与 --allure-stories 是“或”关系,满足其一即被执行

在这里插入图片描述

3.6 @allure.severity装饰器

用于按严重性级别标记测试用例级别

import allure

@allure.severity(allure.severity_level.BLOCKER)
def test_example_for_severity():
    pass

@allure.severity(allure.severity_level.CRITICAL)
def test_example_for_severity():
    pass

其中severity_level的枚举值如下:

class Severity(str, Enum):
    BLOCKER = 'blocker'
    CRITICAL = 'critical'
    NORMAL = 'normal'
    MINOR = 'minor'
    TRIVIAL = 'trivial'

可以使用如下命令行选项指定不同的测试集(可有多个,以逗号分隔

--allure-severities blocker,critical

注意:这个级别的关系是"或"
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2311765.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue2升vue3,uniapp兼容鸿蒙app踩坑记录

前提&#xff1a;最近鸿蒙势头很好&#xff0c;公司的 uniapp vue2 项目&#xff0c;要兼容鸿蒙app。就开始了我的uniapp转鸿蒙踩坑之旅&#xff0c;请看下文&#xff08;注意下文都是在uniapp开发基础上&#xff09; 1. 首先鸿蒙开发只支持Vue3&#xff0c;不支持Vue2、不支持…

DeepSeek × 豆包深度整合指南:工作流全解析

DeepSeek 豆包深度整合指南&#xff1a;工作流全解析 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;可以分享一下给大家。点击跳转到网站。 https://www.captainbed.cn/ccc 文章目录 DeepSeek 豆包深度整合指南&#xff1a;工…

海思Hi3516DV300交叉编译opencv

OpenCV是一个开源的跨平台计算机视觉库&#xff0c;支持C、Python等多种语言&#xff0c;适用于图像处理、目标检测、机器学习等任务。其核心由C编写&#xff0c;高效轻量&#xff0c;提供实时视觉处理功能&#xff0c;广泛应用于工业自动化、医疗影像等领域。 1 环境准备 1…

[FE] React 初窥门径(五):React 组件的加载过程(commit 阶段)

1. 回顾 前一篇文章我们看到&#xff0c;ReactDOM.render 总共包含这些步骤&#xff0c; 然后介绍了 performSyncWorkOnRoot 做的事情&#xff0c;它主要做了两件事&#xff0c; renderRootSync 可称之为 render 阶段&#xff1a;创建了一颗 Fiber Tree&#xff08;包含 html …

java环境部署

java环境部署 一、准备工作 jrejdkeclipse jdk下载&#xff1a;21和1.8-----官网&#xff1a;Oracle&#xff1a;Java 下载 |神谕 该处选择要依据自身的系统类型选择下载 idea的下载安装&#xff1a;IntelliJ IDEA | Other Versions 二、安装 三、环境配置 四、使用 五、i…

100天精通Python(爬虫篇)——第115天:爬虫在线小工具_Curl转python爬虫代码工具(快速构建初始爬虫代码)

文章目录 一、curl是什么&#xff1f;二、爬虫在线小工具&#xff08;牛逼puls&#xff09;三、实战操作 一、curl是什么&#xff1f; 基本概念&#xff1a;curl 支持多种协议&#xff0c;如 HTTP、HTTPS、FTP、SFTP 等&#xff0c;可用于从服务器获取数据或向服务器发送数据&a…

python-leetcode-解决智力问题

2140. 解决智力问题 - 力扣&#xff08;LeetCode&#xff09; 这道题是一个典型的 动态规划&#xff08;Dynamic Programming, DP&#xff09; 问题&#xff0c;可以使用 自底向上 的方式解决。 思路 定义状态&#xff1a; 设 dp[i] 表示从第 i 题开始&#xff0c;能获得的最高…

SpireCV荣获Gitee 最有价值开源项目称号

什么是GVP&#xff1f; GVP全称Gitee Valuable Project&#xff0c;意思为Gitee最有价值开源项目。作为GVP称号的获得者&#xff0c;SpireCV在开源社区中展现出了卓越的实力和影响力&#xff0c;为开源软件的发展和推广做出了积极的贡献。 这一荣誉不仅充分肯定了过去阿木实验…

数据结构基础(一)

文章目录 1 数据结构基础1.1 什么是程序&#xff1f;1.2 数据、数据元素、数据项、数据对象1.3 基本的逻辑结构 2 算法效率2.1 时间复杂度2.1.1 循环执行次数2.1.2 大O(n)表示法 2.2 空间复杂度 1 数据结构基础 1.1 什么是程序&#xff1f; ​ 程序 数据结构 &#xff0b; 算…

⭐算法OJ⭐N-皇后问题 II【回溯剪枝】(C++实现)N-Queens II

⭐算法OJ⭐N-皇后问题【回溯剪枝】&#xff08;C实现&#xff09;N-Queens 问题描述 The n-queens puzzle is the problem of placing n n n queens on an n n n \times n nn chessboard such that no two queens attack each other. Given an integer n, return the num…

项目管理工具 Maven

目录 1.Maven的概念 1.1​​​​​什么是Maven 1.2什么是依赖管理 1.3什么是项目构建 1.4Maven的应用场景 1.5为什么使用Maven 1.6Maven模型 2.初识Maven 2.1Maven安装 2.1.1安装准备 2.1.2Maven安装目录分析 2.1.3Maven的环境变量 2.2Maven的第一个项目 2.2.1按照约…

国产编辑器EverEdit - 宏功能介绍

1 宏 1.1 应用场景 宏是一种重复执行简单工作的利器&#xff0c;可以让用户愉快的从繁琐的工作中解放出来&#xff0c;其本质是对键盘和菜单的操作序列的录制&#xff0c;并不会识别文件的内容&#xff0c;属于无差别无脑执行。 特别是对一些有规律的重复按键动作&#xff0c;…

“双碳”背景下,企业应该如何提升能源效率?

在当今竞争激烈的市场环境中&#xff0c;企业不仅需要优化成本&#xff0c;还需积极响应国家的能源政策&#xff0c;减少对环境的影响。提升工业能源效率正是实现这一双重目标的关键。中国近年来大力推进“双碳”目标&#xff08;碳达峰、碳中和&#xff09;&#xff0c;并出台…

文献学习——考虑混合储能系统选择的基于改进蜂群算法的热电联产微网多目标经济优化调度

摘要&#xff1a;在考虑混合储能系统模型选择的基础上&#xff0c;基于改进的人工蜂群算法&#xff08;ABC&#xff09;&#xff0c;建立了冷热电联产微电网经济优化的多目标调度模型。为了对以往研究中的单目标模型进行升级&#xff0c;将模型的优化目标设定为微电网的日发电调…

nnMamba:基于状态空间模型的3D生物医学图像分割、分类和地标检测

摘要 本文提出了一种基于状态空间模型&#xff08;SSMs&#xff09;的创新架构——nnMamba&#xff0c;用于解决3D生物医学图像分割、分类及地标检测任务中的长距离依赖建模难题。nnMamba结合了卷积神经网络&#xff08;CNN&#xff09;的局部特征提取能力与SSMs的全局上下文建…

安科瑞新能源充电桩解决方案:驱动绿色未来,赋能智慧能源

安科瑞顾强 引言 在“双碳”目标与新能源汽车产业高速发展的双重驱动下&#xff0c;充电基础设施正成为能源转型的核心环节。安科瑞电气股份有限公司凭借在电力监控与能效管理领域20余年的技术积淀&#xff0c;推出新一代新能源充电桩解决方案&#xff0c;以智能化、高兼容性…

使用开源OPUS-MT模型进行文本翻译(python)

1. 环境准备 pip install transformers 2. 下载机器翻译模型&#xff1a; 2.1 代码从hugging face平台下载 from transformers import MarianMTModel, MarianTokenizer# 指定模型名称 model_name "Helsinki-NLP/opus-mt-zh-en" # 中译英模型# 下载并保存分词器到…

Elastic如何获取当前系统时间

文章目录 1. 使用 _ingest.timestamp 在 Ingest Pipeline 中获取当前时间2. 使用 Painless Script 获取当前时间3. 使用 now 关键字在查询中获取当前时间4. 使用 date 类型字段的默认值5. 使用 Kibana 的 Dev Tools 查看当前时间6. 使用 date 聚合获取当前时间7. 使用 Elastics…

jenkins配置连接k8s集群

jenkins配置连接k8s集群 前言 我这边jenkins是在一个服务器里面&#xff0c;k8s集群在其他服务器&#xff0c;实现连接 首先jenkins下载有k8s插件 进入配置页面 获取k8s-api-server地址 对应k8s服务器执行 kubectl config view --minify -o jsonpath{.clusters[0].cluste…

如何选择缓存模式?

如何选择缓存模式 当一个系统引入缓存后&#xff0c;最大的挑战之一便是如何确保缓存与后端数据库的一致性。目前&#xff0c;常见的解决方案主要有Cache Aside、Read/Write Throught和Write Back这三种缓存更新策略。 Read/Write Throught策略 读操作方面&#xff0c;如果缓…