Pytest測(cè)試報(bào)告工具Allure的高級(jí)用法
Allure除了具有Pytest基本狀態(tài)外,其他幾乎所有功能也都支持。
1、嚴(yán)重性
如果你想對(duì)測(cè)試用例進(jìn)行嚴(yán)重等級(jí)劃分,可以使用@allure.severity裝飾器,它可以應(yīng)用于函數(shù),方法或整個(gè)類。
它以allure.severity_level枚舉值作為參數(shù),分別為:BLOCKER(中斷),CRITICAL(嚴(yán)重),NORMAL(常規(guī)),MINOR(輕微),TRIVIAL(不重要)。
示例:
# test_sample.py
import allure
# 兩數(shù)相加
def add(x, y):
return x + y
# 測(cè)試類
@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運(yùn)行:
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 ===========================================================================
報(bào)告:

你還可以通過--allure-severities選項(xiàng)指定嚴(yán)重等級(jí)運(yùn)行,多個(gè)以逗號(hào)分隔。
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、功能
如果你想對(duì)測(cè)試功能、測(cè)試場(chǎng)景進(jìn)行行為描述,可以分別使用裝飾器:@allure.feature和@allure.story。
示例:
# test_sample.py
import allure
# 兩數(shù)相加
def add(x, y):
return x + y
@allure.feature('測(cè)試類')
class TestAdd:
@allure.story('01測(cè)試兩個(gè)正數(shù)相加')
def test_first(self):
assert add(3, 4) == 7
@allure.story('02測(cè)試負(fù)數(shù)正數(shù)相加')
def test_second(self):
assert add(-3, 4) == 1
@allure.story('03測(cè)試正數(shù)負(fù)數(shù)相加')
def test_three(self):
assert add(3, -4) == -1
@allure.story('04測(cè)試兩個(gè)負(fù)數(shù)相加')
def test_four(self):
assert add(-3, -4) == -7運(yùn)行:
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 ===========================================================================
報(bào)告:

你也可以通過--allure-features和--allure-stories選擇指定具體功能和故事運(yùn)行,多個(gè)以逗號(hào)分隔。
E:\workspace-py\Pytest>pytest test_sample.py --allure-stories 01測(cè)試兩個(gè)正數(shù)相加 ========================================================================== 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、步驟
如果你想對(duì)每個(gè)測(cè)試調(diào)用進(jìn)行非常詳細(xì)的逐步說明,可以通過@allure.step裝飾器來實(shí)現(xiàn)(固件同樣支持)。
該裝飾器會(huì)將方法或函數(shù)的調(diào)用與提供的參數(shù)一起添加到報(bào)表中,并且可以包含一條描述行,該行支持位置和關(guān)鍵字參數(shù)。
示例:
# test_sample.py
import pytest
import allure
@allure.step('兩數(shù)相加:{0} + {y}')
def add(x, y):
r = x + y
print_res(r)
return r
@allure.step
def print_res(r):
print('計(jì)算結(jié)果:', 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]報(bào)告:

4、標(biāo)題
如果你想讓測(cè)試標(biāo)題更具可讀性,可以使用@allure.title裝飾器,該裝飾器支持參數(shù)的占位符并支持動(dòng)態(tài)替換。
示例:
# 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("測(cè)試用例-{data}")
@pytest.mark.parametrize("data", data)
def test_add(self, data):
assert add(data[0], data[1]) == data[2]報(bào)告:

5、描述
如果你想添加測(cè)試的詳細(xì)說明,可以通過添加測(cè)試方法描述信息,也可以使用裝飾器@allure.description和@allure.description_html。
示例:
# test_sample.py
import allure
# 被測(cè)功能
def add(x, y):
return x + y
# 測(cè)試類
class TestLearning:
@allure.description('測(cè)試正數(shù)相加')
def test_first(self):
assert add(3, 4) == 7
@allure.description_html('<h1> 測(cè)試負(fù)數(shù)相加 </h1>')
def test_second(self):
"""你也可以在這里添加用例的描述信息,但是會(huì)被allure裝飾器覆蓋"""
assert add(-3, -4) == -7報(bào)告:

6、附件
如果你想在報(bào)告中顯示不同類型的附件,可以通過以下兩種方式來實(shí)現(xiàn):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> 測(cè)試頁面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)報(bào)告:

7、鏈接
如果你想關(guān)聯(lián)缺陷追蹤系統(tǒng)或測(cè)試管理系統(tǒng),你可以使用裝飾器@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', '測(cè)試用例標(biāo)題')
def test_with_testcase_link():
pass報(bào)告:

注意:
@allure.issue 將提供帶有小錯(cuò)誤圖標(biāo)的鏈接,該描述符將測(cè)試用例ID作為輸入?yún)?shù),以將其與提供的問題鏈接類型的鏈接模板一起使用。
鏈接模板在 --allure-link-patternPytest 的配置選項(xiàng)中指定。鏈接模板和類型必須使用冒號(hào)指定:
pytest test_sample.py --alluredir=report --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
官方文檔地址:https://docs.qameta.io/allure/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python執(zhí)行時(shí)間的計(jì)算方法小結(jié)
這篇文章主要介紹了Python執(zhí)行時(shí)間的計(jì)算方法小結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03
一行代碼解決動(dòng)態(tài)執(zhí)行Python函數(shù)方法實(shí)例
這篇文章主要為大家介紹了如何用一行代碼解決動(dòng)態(tài)執(zhí)行Python函數(shù)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
python tkinter制作用戶登錄界面的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了python tkinter制作用戶登錄界面的簡(jiǎn)單實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
python實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到excel的示例--普通格式
今天小編就為大家分享一篇python實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到excel的示例--普通格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Matplotlib實(shí)戰(zhàn)之柱狀圖繪制詳解
柱狀圖,是一種使用矩形條,對(duì)不同類別進(jìn)行數(shù)值比較的統(tǒng)計(jì)圖表,這篇文章主要為大家詳細(xì)介紹了如何使用Matplotlib繪制柱狀圖,需要的可以參考下2023-08-08
Python??序列化反序列化和異常處理的問題小結(jié)
這篇文章主要介紹了Python?序列化反序列化和異常處理,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
關(guān)于torch.scatter與torch_scatter庫的使用整理
這篇文章主要介紹了關(guān)于torch.scatter與torch_scatter庫的使用整理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

