Allure除了具有Pytest基本狀態外,其他幾乎所有功能也都支持。
1、嚴重性
如果你想對測試用例進行嚴重等級划分,可以使用 @allure.severity 裝飾器,它可以應用於函數,方法或整個類。
它以 allure.severity_level 枚舉值作為參數,分別為:BLOCKER(中斷),CRITICAL(嚴重),NORMAL(常規),MINOR(輕微),TRIVIAL(不重要)。
示例:
# test_sample.py import allure # 兩數相加 def add(x, y): return x + y # 測試類 @allure.severity(allure.severity_level.TRIVIAL) class TestAdd: @allure.severity(allure.severity_level.MINOR) def test_first(self): assert add(3, 4) == 7 @allure.severity(allure.severity_level.NORMAL) def test_second(self): assert add(-3, 4) == 1 @allure.severity(allure.severity_level.CRITICAL) def test_three(self): assert add(3, -4) == -1 @allure.severity(allure.severity_level.BLOCKER) def test_four(self): assert add(-3, -4) == -7
運行:
E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir ========================================================================== test session starts ========================================================================== platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0 rootdir: E:\workspace-py\Pytest plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0 collected 4 items test_sample.py .... [100%] =========================================================================== 4 passed in 0.06s ===========================================================================
報告:
你還可以通過 --allure-severities 選項指定嚴重等級運行,多個以逗號分隔。
E:\workspace-py\Pytest>pytest test_sample.py --allure-severities normal,critical ========================================================================== test session starts ========================================================================== platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0 rootdir: E:\workspace-py\Pytest plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0 collected 4 items test_sample.py .. [100%] =========================================================================== 2 passed in 0.02s ===========================================================================
2、功能
如果你想對測試功能、測試場景進行行為描述,可以分別使用裝飾器:@allure.feature 和 @allure.story 。
示例:
# test_sample.py import allure # 兩數相加 def add(x, y): return x + y @allure.feature('測試類') class TestAdd: @allure.story('01測試兩個正數相加') def test_first(self): assert add(3, 4) == 7 @allure.story('02測試負數正數相加') def test_second(self): assert add(-3, 4) == 1 @allure.story('03測試正數負數相加') def test_three(self): assert add(3, -4) == -1 @allure.story('04測試兩個負數相加') def test_four(self): assert add(-3, -4) == -7
運行:
E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir ========================================================================== test session starts ========================================================================== platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0 rootdir: E:\workspace-py\Pytest plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0 collected 4 items test_sample.py .... [100%] =========================================================================== 4 passed in 0.06s ===========================================================================
報告:
你也可以通過 --allure-features 和 --allure-stories 選擇指定具體功能和故事運行,多個以逗號分隔。
E:\workspace-py\Pytest>pytest test_sample.py --allure-stories 01測試兩個正數相加 ========================================================================== test session starts ========================================================================== platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0 rootdir: E:\workspace-py\Pytest plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0 collected 4 items test_sample.py . [100%] =========================================================================== 1 passed in 0.02s ===========================================================================
3、步驟
如果你想對每個測試調用進行非常詳細的逐步說明,可以通過 @allure.step 裝飾器來實現(固件同樣支持)。
該裝飾器會將方法或函數的調用與提供的參數一起添加到報表中,並且可以包含一條描述行,該行支持位置和關鍵字參數。
示例:
# test_sample.py import pytest import allure @allure.step('兩數相加:{0} + {y}') def add(x, y): r = x + y print_res(r) return r @allure.step def print_res(r): print('計算結果:', r) class TestLearning: data = [ [3, 4, 7], [-3, 4, 1], [3, -4, -1], [-3, -4, -7], ] @pytest.mark.parametrize("data", data) def test_add(self, data): assert add(data[0], data[1]) == data[2]
報告:
4、標題
如果你想讓測試標題更具可讀性,可以使用 @allure.title 裝飾器,該裝飾器支持參數的占位符並支持動態替換。
示例:
# test_sample.py import pytest import allure def add(x, y): return x + y class TestLearning: data = [ [3, 4, 7], [-3, 4, 1], [3, -4, -1], [-3, -4, -7], ] @allure.title("測試用例-{data}") @pytest.mark.parametrize("data", data) def test_add(self, data): assert add(data[0], data[1]) == data[2]

5、描述
如果你想添加測試的詳細說明,可以通過添加測試方法描述信息,也可以使用裝飾器 @allure.description 和 @allure.description_html 。
示例:
# test_sample.py import allure # 被測功能 def add(x, y): return x + y # 測試類 class TestLearning: @allure.description('測試正數相加') def test_first(self): assert add(3, 4) == 7 @allure.description_html('<h1> 測試負數相加 </h1>') def test_second(self): """你也可以在這里添加用例的描述信息,但是會被allure裝飾器覆蓋""" assert add(-3, -4) == -7
報告:
6、附件
如果你想在報告中顯示不同類型的附件,可以通過以下兩種方式來實現:
allure.attach(body, name, attachment_type, extension)
allure.attach.file(source, name, attachment_type, extension)
示例:
import allure
def test_multiple_attachments(): allure.attach.file(r'C:\Users\Public\Pictures\Sample Pictures\Koala.jpg', attachment_type=allure.attachment_type.JPG) allure.attach('<head></head><body> 測試頁面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)
報告:
7、鏈接
如果你想關聯缺陷追蹤系統或測試管理系統,你可以使用裝飾器 @allure.link、@allure.issue、@allure.testcase。
示例:
import allure @allure.link('http://www.baidu.com') def test_with_named_link(): pass @allure.issue('101', '缺陷問題描述') def test_with_issue_link(): pass @allure.testcase('http://this.testcase.com', '測試用例標題') def test_with_testcase_link(): pass
報告:

注意:
@allure.issue 將提供帶有小錯誤圖標的鏈接,該描述符將測試用例ID作為輸入參數,以將其與提供的問題鏈接類型的鏈接模板一起使用。
鏈接模板在 --allure-link-patternPytest 的配置選項中指定。鏈接模板和類型必須使用冒號指定:
pytest test_sample.py --alluredir=report --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
官方文檔地址:https://docs.qameta.io/allure/#_pytest
作者:Leozhanggg
出處:https://www.cnblogs.com/leozhanggg/p/14059844.html
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。