Pytest系列(23)- allure 打标记之 @allure.epic()、@allure.feature()、@allure.story() 的详细使用

简介: Pytest系列(23)- allure 打标记之 @allure.epic()、@allure.feature()、@allure.story() 的详细使用

如果你还想从头学起Pytest,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1690628.html

 

前言


  • 前面几篇文章主要介绍了allure的特性,这篇文章我们就来讲下allure的标记用法
  • 有时候我们写pytest的时候,会用到 @pytest.mark 但并不会显示在allure报告上
  • 而allure也提供了三种类型的标记装饰器,它们是可以显示在allure报告上的

 

allure的标记装饰器


  • BDD样式的标记装饰器
  • 优先级(严重程度)标记装饰器
  • 自定义标记装饰器

 

BDD标记装饰器


提供了三个装饰器

  • @allure.epic:敏捷里面的概念,定义史诗,往下是 feature
  • @allure.feature:功能点的描述,理解成模块往下是 story
  • @allure.story:故事,往下是 title

 

栗子一(story+feature)


测试代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__  =
__Time__   = 2020-04-19 14:27
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import allure
def test_without_any_annotations_that_wont_be_executed():
    pass
@allure.story('epic_1')
def test_with_epic_1():
    pass
@allure.story('story_1')
def test_with_story_1():
    pass
@allure.story('story_2')
def test_with_story_2():
    pass
@allure.feature('feature_2')
@allure.story('story_2')
def test_with_story_2_and_feature_2():
    pass


无标记装饰器

我们先看看没有设置标记装饰器时,allure报告是咋样的

image.png

image.png

添加装饰器

加了 @allure.feature 和 @allure.story 之后的 allure 报告

image.png

image.png

知识点

  • story 是 feature 的子集,当测试用例有 @allure.feature、@allure.story 时,在报告上会先显示 feature,点开之后再显示 story【可以想象成,安徒生童话(feature)有很多个童话故事(story)】
  • 如果不加 @allure.feature、@allure.story 时,在Behaviors栏目下,测试用例都不会分类显示,当用例多的时候可能看的花里胡哨

 

栗子二


前言

这里应用了包括前面所讲的全部装饰器

 

测试代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__  = 
__Time__   = 2020/10/27 19:44
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import os
import allure
import pytest
@pytest.fixture(scope="session")
def login_fixture():
    print("=== 前置登录 ===")
@allure.step("步骤1")
def step_1():
    print("操作步骤---------------1")
@allure.step("步骤2")
def step_2():
    print("操作步骤---------------2")
@allure.step("步骤3")
def step_3():
    print("操作步骤---------------3")
@allure.epic("epic 相当于总体描述")
@allure.feature("测试模块")
class TestAllureALL:
    @allure.testcase("https://www.cnblogs.com/poloyy/", '测试用例,点我一下')
    @allure.issue("https://www.cnblogs.com/poloyy/p/12219145.html", 'Bug 链接,点我一下')
    @allure.title("用例的标题")
    @allure.story("story one")
    @allure.severity("critical")
    def test_case_1(self, login_fixture):
        """
        小菠萝测试笔记地址:https://www.cnblogs.com/poloyy/
        """
        print("测试用例1")
        step_1()
        step_2()
    @allure.story("story two")
    def test_case_2(self, login_fixture):
        print("测试用例2")
        step_1()
        step_3()
@allure.epic("另一个 epic")
@allure.feature("查找模块")
class TestAllureALL2:
    @allure.story("story three")
    def test_case_3(self, login_fixture):
        print("测试用例3")
        step_1()
    @allure.story("story four")
    def test_case_4(self, login_fixture):
        print("测试用例4")
        step_3()
if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './allure'])
    os.system('allure -c ./allure')
    os.system('allure serve ./allure-report')


allure 报告

image.png

image.png


总结


倘若是用 pytest+allure 写项目,又想用 @pytest.mark.xxx 来给不同的用例添加标记的话,可以尝试用 @allure.feature、@allure.story 替换,毕竟可以显示在报告上

 

提出问题

用命令行方式运行时,可以指定运行某个story、feature、epic吗?

 

自问自答

当然可以,跟 @pytest.mark.xxx 指定标签运行的方式没啥区别,添加下面的命令行参数就行

  • --allure-epics
  • --allure-features
  • --allure-stories

 

举栗子

# 只运行 epic 名为 test 的测试用例
pytest --alluredir ./report/allure --allure-epics=test
# 只运行 feature 名为 模块 的测试用例
pytest --alluredir ./report/allure --allure-features=模块
# 只运行 story1、story2 的测试用例(也可以不用=号 空格就行了哦)
pytest tests.py --allure-stories story1,story2
# 指定 feature+story
pytest tests.py --allure-features feature2 --allure-stories story2


相关文章
|
存储 虚拟化 Docker
windows系统安装docker(Hyper-V方式)
windows系统安装docker(Hyper-V方式)
2918 2
|
存储 算法 数据可视化
python老司机必备-内存泄露分析优化
python老司机必备-内存泄露分析优化
2554 0
python老司机必备-内存泄露分析优化
|
iOS开发 Python
mac:python安装路径
了解python的安装路径,有利于处对python安装的管理。比如删除多余的python,可以在这些路径中查询处理。
2281 0
|
人工智能 自然语言处理 数据可视化
AI 数据分析产品推荐:更高效、更可控的智能报告解决方案
在与客户的共创中,我们发现数据团队仍被困在周报、月报的重复劳动中,AI 生成的报告往往结构松散、缺乏深度,无法直接使用。这引发我们对智能分析范式的重新思考,推出了 「智能融合报告」,确立了一种新的协作方式:您作为“总设计师”编排思路,AI 作为“超级工匠”精准执行。通过这种方式,您能够将业务经验融入分析框架,全程掌控生成过程,获得结构严谨、洞察深入且可复用的分析成果。如果您在寻找更高效、更可控的智能报告解决方案,这篇凝结我们实践思考的文章值得一读。
|
数据挖掘 测试技术 定位技术
《深入剖析:Python自动化测试框架之unittest与pytest》
unittest 和 pytest 是 Python 自动化测试领域的两大利器。unittest 作为标准库的一部分,像一位严谨的“老工匠”,以面向对象的方式提供标准化、可靠的测试基础,适合初学者和小型项目。其 TestCase 类为核心,setup/teardown 方法确保测试独立性。 pytest 则如一位灵活的“艺术家”,以简洁自由的风格和强大功能脱颖而出。它支持参数化测试、灵活的 fixture 机制,并拥有丰富的插件生态,适合复杂场景和大型项目。两者各有优劣,选择需根据项目需求权衡:unittest 稳定兼容,pytest 灵活高效。掌握二者特性,可助力开发者构建高质量测试框架。
826 12
|
12月前
|
XML 人工智能 自然语言处理
禅道文档 300 条用例一键生成:一次看懂爱测智能化测试平台的实力
测试团队面临需求碎片化、迭代加速的挑战,传统用例编写效率瓶颈凸显。爱测智能化测试平台借助生成式AI,实现从需求文档自动生成多场景、多格式、可执行的测试用例。通过大模型理解文档、智能体配置、知识图谱与自然语言驱动执行,平台几分钟内生成近300条高覆盖用例,支持导出至禅道等系统,全链路自动化。未来测试的竞争,是“会用AI”与“不会用AI”的差距。
Appium设置会话时长,解决调试代码时session失效的问题
Appium设置会话时长,解决调试代码时session失效的问题
672 0
|
存储 人工智能 缓存
Mooncake:月之暗面Kimi联合清华等机构推出的大模型推理架构
Mooncake是由月之暗面Kimi联合清华大学等机构共同开源的大模型推理架构。该架构以KVCache为中心,通过分布式设计和资源优化,显著提升了大模型推理的吞吐量和效率,同时降低了算力开销。Mooncake在长上下文场景中表现出色,支持多种应用场景,如自然语言处理、内容推荐系统和搜索引擎等。
2424 6
Mooncake:月之暗面Kimi联合清华等机构推出的大模型推理架构
|
IDE 开发工具 iOS开发
【10月更文挑战第3天】「Mac上学Python 3」入门篇3 - 安装Python与开发环境配置
本篇将详细介绍如何在Mac系统上安装Python,并配置Python开发环境。内容涵盖Python的安装、pip包管理工具的配置与国内镜像源替换、安装与配置PyCharm开发工具,以及通过PyCharm编写并运行第一个Python程序。通过本篇的学习,用户将完成Python开发环境的搭建,为后续的Python编程工作打下基础。
1871 2
【10月更文挑战第3天】「Mac上学Python 3」入门篇3 - 安装Python与开发环境配置
|
测试技术
自动化测试项目学习笔记(五):Pytest结合allure生成测试报告以及重构项目
本文介绍了如何使用Pytest和Allure生成自动化测试报告。通过安装allure-pytest和配置环境,可以生成包含用例描述、步骤、等级等详细信息的美观报告。文章还提供了代码示例和运行指南,以及重构项目时的注意事项。
1971 1
自动化测试项目学习笔记(五):Pytest结合allure生成测试报告以及重构项目