1. 程式人生 > >Pytest系列(23)- allure打標記,@allure.feature()、@allure.story()、@allure.severity()的詳細使用

Pytest系列(23)- allure打標記,@allure.feature()、@allure.story()、@allure.severity()的詳細使用

如果你還想從頭學起Pytest,可以看看這個系列的文章哦!

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

 

前言

  • 前面幾篇文章主要介紹了allure的特性,這篇文章我們就來講下allure的標記用法
  • 有時候我們寫pytest的時候,會用到 @pytest.mark 但並不會顯示在allure報告上
  • 而allure也提供了三種類型的標記裝飾器,它們是可以顯示在allure報告上的

 

allure的標記裝飾器

  • BDD樣式的標記裝飾器
  • 優先順序(嚴重程度)標記裝飾器
  • 自定義標記裝飾器

 

BDD標記裝飾器

提供了兩個裝飾器

  • @allure.feature

  • @allure.story 

直接上程式碼栗子

#!/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報告是咋樣的

 

 

加了@allure.feature和@allure.story之後,allure報告又是怎麼樣的呢

 

知識點

story是feature的子集,當測試用例有 @allure.feature、@allure.story 時,在報告上會先顯示feature,點開之後再顯示story【可以想象成,安徒生童話(feature)有很多個童話故事(story)】

如果不加 @allure.feature、@allure.story 時,在Behaviors欄目下,測試用例都不會分類顯示,當用例多的時候可能看的花裡胡哨

 

總結

倘若是用pytest+allure寫專案的話,又想用@pytest.mark.xxx 來自定義標記的話可以嘗試用 @allure.feature、@allure.story 替換,畢竟可以顯示在報告上

 

問題來了,用命令列方式執行時,可以指定執行某個story或者feature嗎?

答案是:當然可以!!跟@pytest.mark.xxx沒啥區別哦!!

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

譬如

 pytest tests.py --allure-stories story_1,story_2
pytest tests.py --allure-features feature2 --allure-stories story2

 

@ allure.severity

作用:按嚴重性(優先順序)來標記測試用例,它使用allure.severity_level列舉值作為引數

 

先看看列舉類有哪些常量

嚴重程度最高blocker,最低trivial

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

 

看看程式碼栗子

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-19 14:50
__Author__ = 小菠蘿測試筆記
__Blog__   = https://www.cnblogs.com/poloyy/
"""

import allure


def test_with_no_severity_label():
    pass


@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_severity():
    pass


@allure.severity(allure.severity_level.NORMAL)
def test_with_normal_severity():
    pass


@allure.severity(allure.severity_level.NORMAL)
class TestClassWithNormalSeverity(object):

    def test_inside_the_normal_severity_test_class(self):
        pass

    @allure.severity(allure.severity_level.CRITICAL)
    def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self):
        pass

 

執行結果,檢視allure報告

其實就是測試用例多了個優先順序severity屬性而已...

 

命令列方式

也可以通過命令列引數執行指定severity的測試用例哦

pytest tests.py --allure-severities normal,critical

&n