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)告:

你還可以通過(guò)--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)告:

你也可以通過(guò)--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ì)的逐步說(shuō)明,可以通過(guò)@allure.step裝飾器來(lái)實(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ì)說(shuō)明,可以通過(guò)添加測(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)告中顯示不同類型的附件,可以通過(guò)以下兩種方式來(lái)實(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è)試頁(yè)面 </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', '缺陷問(wèn)題描述')
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ù),以將其與提供的問(wèn)題鏈接類型的鏈接模板一起使用。
鏈接模板在 --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中創(chuàng)建表格詳細(xì)過(guò)程
這篇文章主要介紹了Python中創(chuàng)建表格,在python中使用tabulate庫(kù)來(lái)創(chuàng)建表格,并針對(duì)輸出形式進(jìn)行不斷改進(jìn)來(lái)美化輸出效果,并給出了代碼示例。需要的小伙伴可以參考一下2022-02-02
python內(nèi)置數(shù)據(jù)類型之列表操作
數(shù)據(jù)類型是一種值的集合以及定義在這種值上的一組操作。這篇文章主要介紹了python內(nèi)置數(shù)據(jù)類型之列表的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
Python?selenium?get_cookies獲取cookie不全的解決方案
之前使用爬蟲(chóng)時(shí)最讓我頭疼的就是cookie失效的問(wèn)題了,下面這篇文章主要給大家介紹了關(guān)于Python?selenium?get_cookies獲取cookie不全的解決方案,需要的朋友可以參考下2022-10-10
python版本的仿windows計(jì)劃任務(wù)工具
這篇文章主要介紹了python版本的仿windows計(jì)劃任務(wù)工具,計(jì)劃任務(wù)工具根據(jù)自己設(shè)定的具體時(shí)間,頻率,命令等屬性來(lái)規(guī)定所要執(zhí)行的計(jì)劃,當(dāng)然功能不是很全大家可以補(bǔ)充2018-04-04
python讀寫(xiě)修改Excel之xlrd&xlwt&xlutils
這篇文章主要介紹了python讀寫(xiě)修改Excel之xlrd&xlwt&xlutils,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
解決TensorFlow訓(xùn)練內(nèi)存不斷增長(zhǎng),進(jìn)程被殺死問(wèn)題
今天小編就為大家分享一篇解決TensorFlow訓(xùn)練內(nèi)存不斷增長(zhǎng),進(jìn)程被殺死問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

