pytest文檔44-allure.dynamic動態生成用例標題


前言

pytest 結合 allure 描述用例的時候我們一般使用 @allure.title@allure.description 描述測試用例的標題和詳情。
在用例里面也可以動態更新標題和詳情,使用allure.dynamic方法實現。

allure.dynamic 動態屬性

feature 模塊

allure.dynamic.feature(feature_name)

功能點 story

allure.dynamic.story(case_story)

用例標題 title

allure.dynamic.title(case_title)

用例描述:請求URL 請求類型 期望結果 實際結果描述

desc = "<font color='red'>請求URL:</font>{}<Br/>" \
           "<font color='red'>請求類型:</font>{}<Br/>" \
           "<font color='red'>期望結果:</font>{}<Br/>" \
           "<font color='red'>實際結果描述:</font>{}<Br/>".format(url,method,expect,expect_result)
allure.dynamic.description(desc)

description 用例描述

可以在測試主體內部動態更新描述 allure.dynamic.description

import allure

@allure.description("""
This description will be replaced at the end of the test.
""")
def test_dynamic_description():
    assert 42 == int(6 * 7)
    allure.dynamic.description('A final description.')

最后用例的描述被更新為 'A final description.'

title 用例標題

用例標題也可以被動態更新

@allure.title("This title will be replaced in a test body")
def test_with_dynamic_title():
    assert 2 + 2 == 4
    allure.dynamic.title('After a successful test finish, the title was replaced with this line.')

最終用例的title更新為'After a successful test finish, the title was replaced with this line.'

參數化

參數化時候,可以使用@allure.title給用例不同標題

@allure.title("Parameterized test title: adding {param1} with {param2}")
@pytest.mark.parametrize('param1,param2,expected', [
    (2, 2, 4),
    (1, 2, 5)
])
def test_with_parameterized_title(param1, param2, expected):
    assert param1 + param2 == expected

也可以在用例里面使用allure.dynamic.title更新用例的標題

import pytest
import allure
# 作者:上海-悠悠 QQ交流群:779429633

def login(username, password):
    '''登錄'''
    print("輸入賬號:%s" % username)
    print("輸入密碼:%s" % password)
    # 返回
    return {"code": 0, "msg": "success!"}


# 測試數據
test_datas = [
    ({"username": "yoyo1", "password": "123456"}, "success!", "輸入正確賬號,密碼,登錄成功"),
    ({"username": "yoyo2", "password": "123456"}, "failed!", "輸入錯誤賬號,密碼,登錄失敗"),
    ({"username": "yoyo3", "password": "123456"}, "success!", "輸入正確賬號,密碼,登錄成功"),
]


@allure.story("登錄用例")
@pytest.mark.parametrize("test_input,expected,title",
                         test_datas
                         )
def test_login(test_input, expected, title):
    '''測試登錄用例'''
    # 獲取函數返回結果
    result = login(test_input["username"], test_input["password"])
    # 斷言
    assert result["msg"] == expected
    allure.dynamic.title(title)

最終生成報告效果

其它屬性

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM