Pytest学习笔记(15)-allure之@allure.epic()、@allure.feature()和@allure.story()

@allure.epic()、@allure.feature()和@allure.story()

本次主要介绍如何使用allure来灵活的标记测试。

  • Pytest 通过使用标记装饰器@pytest.mark来实现,但是不会展示到测试报告内
  • Allure 则提供了 3 种类型的标记装饰器来标记测试,并且可以同步展示到测试报告内
    • BDD风格的标签
    • 严重程度的标签
    • 自定义的标签
BDD风格的标签
  • @allure.epic:敏捷里面的概念,定义史诗,相当于module级的标签
  • @allure.feature:功能点的描述,可以理解成模块,相当于class级的标签
  • @allure.story:故事,可以理解为场景,相当于method级的标签
源码
# allure.py

from allure_commons._allure import epic, feature, story
# _allure.py

def epic(*epics):
    return label(LabelType.EPIC, *epics)


def feature(*features):
    return label(LabelType.FEATURE, *features)


def story(*stories):
    return label(LabelType.STORY, *stories)


def label(label_type, *labels):
    return safely(plugin_manager.hook.decorate_as_label(label_type=label_type, labels=labels))
# helper.py

@allure_commons.hookimpl
    def decorate_as_label(self, label_type, labels):
        allure_label = getattr(pytest.mark, ALLURE_LABEL_MARK)
        return allure_label(*labels, label_type=label_type)

可以看出epic、feature、story都是调用的label方法,只是用处不同而已,这和前面的link功能非常相似。

官方代码例子
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

"""
    __File__   = test_tags.py
    __Project__= _JAuto-Interface
    __Time__   = 2022-02-17 17:49:03
    __Author__ = 黎晟
"""

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.epic('epic_2')
@allure.feature('feature_2')
@allure.story('story_2')
def test_with_epic_2_story_2_and_feature_2():
    pass
执行结果

先看下未使用标记的效果
在这里插入图片描述在这里插入图片描述

使用epic、feature、story装饰器的效果
在这里插入图片描述
在这里插入图片描述

小结一下
  • 关系:epic是feature父级,feature是story父级,是包含关系,效果跟书籍的目录或者项目结构相似
  • 它们可以同时使用,也可以单独使用,也可以不使用
  • 如果使用allure和pytest来组织的自动化框架,推荐使用allure的标记来标记用例,替换@pytest.mark.xxx,因为功能一致,且allure 标记功能可以直接展示到报告内
  • 另外提一嘴,这三种标记方式都支持汉字哦,举个🌰:@allure.feature(‘模块级’)
拓展:命令行方式

@pytest.mark.xxx相同,也可以通过命令行来运行指定epic、feature、story标记的用例:

  • –allure-epics

  • –allure-features

  • –allure-stories

举个🌰
# 只运行 epic 名为 epic1 的测试用例
pytest --alluredir ./report/allure --allure-epics=epic1

# 只运行 feature 名为 模块级 的测试用例
pytest --alluredir ./report/allure --allure-features=模块级

# 只运行 story1、story2 的测试用例(也可以不用=号 空格即可)
pytest tests.py --allure-stories story1,story2

# 指定 feature和story
pytest tests.py --allure-features feature1,feature2 --allure-stories story1
  • 10
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值