@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