Skip to content

Allure2 报告中添加用例分类


简介

在测试过程中根据项目需要可以为项目,以及项目下的不同模块对用例进行分类管理。也可以只运行某个类别下的用例。


Allure 添加用例分类

  • @allure.epic:“史诗”,敏捷里面的概念,相当于定义一个项目。
  • @allure.feature:“特征”,相当于一个功能模块或者测试套件,可以管理很多个子分支 story。
  • @allure.story:“故事”,相当于对应这个功能或者模块下的不同场景,分支功能。

epic 与 feature,feature 与 story 类似于父子关系。


Allure 分类 - epic

如果要测试报告中看到用例所在的项目,需要用到 epic。这种方法一般运用在类上,很少用在测试用例上,相当于定义一个项目的需求。由于粒度比较大,在 epic 下面还要定义略小粒度的用户故事。

语法:@allure.epic("项目需求描述")

import allure

@allure.epic("需求1")
class TestEpic:

    def test_case1(self):
        print("用例1")

    def test_case2(self):
        print("用例2")

    def test_case3(self):
        print("用例3")

Allure 分类 - feature

如果要在报告中看到测试的功能模块描述,可以使用 @allure.Feature

一般用在测试类上。

语法:@allure.feature('功能名称')

import allure

@allure.epic("需求1")
@allure.feature("功能模块1")
class TestEpic:

    @allure.title("用例1")
    def test_case1(self):
        print("用例1")

    @allure.title("用例2")
    def test_case2(self):
        print("用例2")

    @allure.title("用例3")
    def test_case3(self):
        print("用例3")

Allure 分类 - story

如果要在报告中看到测试的子功能的描述,可以使用 @allure.story

一般用在测试方法上。

语法:@allure.story('子功能名称')

import allure

@allure.epic("需求1")
@allure.feature("功能模块1")
class TestEpic:

    @allure.story("子功能1")
    @allure.title("用例1")
    def test_case1(self):
        print("用例1")

    @allure.story("子功能2")
    @allure.title("用例2")
    def test_case2(self):
        print("用例2")

    @allure.story("子功能2")
    @allure.title("用例3")
    def test_case3(self):
        print("用例3")

执行指定分类的用例

  • allure 相关的命令查看:
    • mac 系统:pytest --help | grep allure
    • windows 系统:pytest --help | findstr allure
pytest --help | grep allure
  --allure-severities=SEVERITIES_SET
  --allure-epics=EPICS_SET
  --allure-features=FEATURES_SET
  --allure-stories=STORIES_SET
  --allure-ids=IDS_SET  Comma-separated list of IDs.
  --allure-label=LABELS_SET
  --allure-link-pattern=LINK_TYPE:LINK_PATTERN
  --alluredir=DIR       Generate Allure report in the specified directory (may
  --clean-alluredir     Clean alluredir folder if it exists
  --allure-no-capture   Do not attach pytest captured logging/stdout/stderr to

  • 通过指定命令行参数,运行 epic/feature/story 相关的用例。
# 只运行 epic 名为 "需求1" 的测试用例
pytest --alluredir ./results --clean-alluredir --allure-epics=需求1

# 只运行 feature 名为 "功能模块2" 的测试用例
pytest --alluredir ./results --clean-alluredir --allure-features=功能模块2

# 只运行 story 名为 "子功能1" 的测试用例
pytest --alluredir ./results --clean-alluredir --allure-stories=子功能1

# 运行 story 名为 "子功能1和子功能2" 的测试用例
pytest --alluredir ./results --clean-alluredir --allure-stories=子功能1,子功能2

# 运行 feature + story 的用例(取并集)
pytest --alluredir ./results --clean-alluredir --allure-features=功能模块1 --allure-stories=子功能1,子功能2

总结

  • Allure 分类描述。
  • Allure 运行指定分类。