1. 程式人生 > 實用技巧 >Pytest 系列(29)- 詳解 allure.dynamic 動態生成功能

Pytest 系列(29)- 詳解 allure.dynamic 動態生成功能

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

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

前言

  • @allure.title@allure.description都是裝飾器,給測試用例提供標題和描述
  • 其實 allure 還提供了在測試用例執行過程中動態指定標題和描述等標籤的方法
  • 如:allure.dynamic.descriptionallure.dynamic.title

allure.dynamic 的原始碼

class Dynamic(object):

    @staticmethod
    def title(test_title):
        plugin_manager.hook.add_title(test_title
=test_title) @staticmethod def description(test_description): plugin_manager.hook.add_description(test_description=test_description) @staticmethod def description_html(test_description_html): plugin_manager.hook.add_description_html(test_description_html=test_description_html) @staticmethod
def label(label_type, *labels): plugin_manager.hook.add_label(label_type=label_type, labels=labels) @staticmethod def severity(severity_level): Dynamic.label(LabelType.SEVERITY, severity_level) @staticmethod def feature(*features): Dynamic.label(LabelType.FEATURE,
*features) @staticmethod def story(*stories): Dynamic.label(LabelType.STORY, *stories) @staticmethod def tag(*tags): Dynamic.label(LabelType.TAG, *tags) @staticmethod def link(url, link_type=LinkType.LINK, name=None): plugin_manager.hook.add_link(url=url, link_type=link_type, name=name) @staticmethod def issue(url, name=None): Dynamic.link(url, link_type=LinkType.ISSUE, name=name) @staticmethod def testcase(url, name=None): Dynamic.link(url, link_type=LinkType.TEST_CASE, name=name)

重點

上面有的方法都能進行動態修改,如:

allure.dynamic.feature
allure.dynamic.link
allure.dynamic.issue
allure.dynamic.testcase
allure.dynamic.story
allure.dynamic.title
allure.dynamic.description

title 的栗子

測試程式碼

@allure.title("裝飾器標題")
def test_1():
    print(123)
    allure.dynamic.title("動態標題")

allure 報告

description 的栗子

測試程式碼

def test_1():
    """
    動態設定描述
    """
    print(123)
    allure.dynamic.description("動態描述")
    allure.dynamic.title("動態標題")

allure 報告

可以看到動態描述會覆蓋動態設定描述

結合parametrize

測試程式碼

data = [
    ("name1", "123456", "name1 登入成功"),
    ("name2", "123456", "name2 登入失敗"),
    ("name3", "123456", "name3 登入成功")
]


@pytest.mark.parametrize('username,pwd,title', data)
def test_2(username, pwd, title):
    """
    登入測試用例1
    """
    print(username, pwd)
    allure.dynamic.title(title)

allure 報告

其他屬性的栗子

測試程式碼

def test_2():
    allure.dynamic.feature('動態feature')
    allure.dynamic.story('動態story')
    allure.dynamic.link("https://www.cnblogs.com/poloyy/p/1.html", '動態Link')
    allure.dynamic.issue("https://www.cnblogs.com/poloyy/p/2.html", '動態Issue')
    allure.dynamic.testcase("https://www.cnblogs.com/poloyy/p/3.html", '動態testcase')

allure 報告