一、allure的介紹
Allure 是由Java 語言開發(fā)的一個輕量級,靈活的測試報告工具。
Allure多平臺的 Report框架。
Allure 支持多語言,包括 python、JaveScript、PHP、Ruby 等。
可以為開發(fā)/測試/管理等人員提供詳盡的的測試報告,包括測試類別、測試步驟、日志、圖片、視
頻等。
可以為管理層提供高水準(zhǔn)的統(tǒng)計報告。
可以集成到Jenkins生成在線的趨勢匯總報告。
二、allure的運行方式
收集結(jié)果:
pytest 測試模塊/測試包/測試用例 --alluredir = 指定存儲測試結(jié)果的路徑
pytest --alluredir=./reports --clean-alluredir
生成在線的測試報告:
allure serve ./results
安裝allure-pytest
三、allure報告的生成
方式一、在線報告、會直接打開默認(rèn)瀏覽器展示當(dāng)前報告
--clean-alluredir
:清理已經(jīng)生成的報告的歷史記錄
1、pytest 測試模塊/測試包/測試用例 --alluredir = 指定存儲測試結(jié)果的路徑
2、生成在線的測試報告:
allure serve ./results
方式二、靜態(tài)資源文件報告(帶index.html、css、js等文件),需要將報告布置到web服務(wù)器上。
應(yīng)用場景:如果希望隨時打開報告,可以生成一個靜態(tài)資源文件報告,將這個報告布署到web服務(wù)器上,啟動web服務(wù),即可隨時隨地打開報告。
解決方案:使用allure generate 生成帶有 index.html 的結(jié)果報告。這種方式需要
兩個步驟:
第一步:生成報告。
--clean:如果報告路徑重復(fù),清理上一次的報告
allure generate ./results --clean
-o:將測試報告生成指定的目錄
allure generate ./results --clean -o ./reports
第二步:打開報告。
allure open allure-report
打開報告指定IP地址和端口號
-h 127.0.0.1 -p 8888
allure open ./reports -h 127.0.0.1 -p 8888
四、allure中裝飾器
@allure.epic() epic描述 敏捷里面的概念,定義史詩,往下是feature
@allure.feature() 模塊名稱 功能點的描述,往下是 story
@allure.story() 用戶故事 用戶故事,往下是title
@allure.title(用例的標(biāo)題)用例的標(biāo)題 重命名 html 報告名稱
@allure.step() 操作步驟 測試用例的步驟
@allure.testcase() 測試用例的鏈接地址對應(yīng)功能測試用例系統(tǒng)里面的case
@allure.issue() 缺陷 對應(yīng)缺陷管理系統(tǒng)里面的鏈接
@allure.description() 用例描述 測試用例的描述
@allure.severity() 用例等級 blocker, critical, normal, minor, trivial
@allure.link() 鏈接 定義一個鏈接,在測試報告展現(xiàn)
@allure.attachment() 附件 報告添加附件
1、實現(xiàn)給測試報告添加用例標(biāo)題
通過使用裝飾器@allure.title可以為測試用例自定義一個可閱讀性的標(biāo)題。
allure.title的三種使用方式:
a.直接使用@allure.title為測試用例自定義標(biāo)題。
import allure
@allure.title('測試標(biāo)題')
def test_with_title():
assert True
b.@allure.title支持通過占位符的方式傳遞參數(shù),可以實現(xiàn)測試用例標(biāo)題參數(shù)化,動態(tài)生成測試用例標(biāo)題。
@allure.title("參數(shù)化用例標(biāo)題:參數(shù)1:{p1},參數(shù)2:{p2}")
@pytest.mark.parametrize('p1,p2,p3',[[1,1,2],[2,4,6]])
def test_with_title1(p1,p2,p3):
assert p1+p2==p3
c.allure.dynamic.title動態(tài)更新測試用例標(biāo)題。
@allure.title('原始標(biāo)題')
def test_with_title2():
assert True
allure.dynamic.title('更改后的標(biāo)題')
2、allure報告中添加用例步驟
應(yīng)用場景:編寫自動化測試用例的時候經(jīng)常會遇到需要編寫流程性測試用例的場景,一般流程性的測試用例的測試步驟比較多,我們在測試用例中添加詳細(xì)的步驟會提高測試用例的可閱讀性。
方法一:使用裝飾器定義一個測試步驟,在測試用例中使用。
import allure
import pytest
@allure.step
def simple_step1(step_param1,step_param2=None):
"""定義一個測試步驟"""
print(f'步驟1:打開頁面,參數(shù)1:{step_param1},參數(shù)2:{step_param2}')
@allure.step
def simple_step2(step_param):
"""定義一個測試步驟"""
print(f'步驟1:完成搜索{step_param}功能')
@pytest.mark.parametrize('param1',['pytest','allure'],ids=['search pytest','search pytest'])
def test_parameterize_with_id(param1):
simple_step2(param1)
@pytest.mark.parametrize('param1',[True, False])
@pytest.mark.parametrize('param2', ['value 1', 'value 2'])
def test_parametrize_with_two_parameters(param1, param2):
simple_step1(param1,param2)
@pytest.mark.parametrize('param2',['pytest', 'unittest'])
@pytest.mark.parametrize('param1,param3', [[1,2]])
def test_parameterize_with_uneven_value_sets(param1, param2, param3):
simple_step1(param1,param3)
simple_step2(param2)
方法二:使用with allure.step()添加測試步驟。
@allure.title("搜索用例:{searchkey}")
@pytest.mark.parametrize("searchkey",['java','allure'])
def test_step_in_method(searchkey):
with allure.step("測試步驟一:打開頁面"):
print("操作a")
print("操作b")
with allure.step(f"測試步驟二:搜索{searchkey}"):
print(f"搜索操作{searchkey}")
with allure.step("測試步驟三:斷言"):
assert True
3、allure報告中添加用例鏈接
應(yīng)用場景:將報告與bug管理系統(tǒng)或測試用例管理系統(tǒng)集成,可以添加鏈接裝飾器
allure. link、@allure.issue和allure.testcase。
格式1: @allure.link (url, name)添加一個普通的link鏈接,name:起別名
import allure
@allure.link('http://www.baidu.com',name='這是一個鏈接')
def test_with_link():
pass
格式2: allure.testcase( url,name)添加一個用例管理系統(tǒng)鏈接。
TEST_CASE_LINK='http://www.baidu.com'
@allure.testcase(TEST_CASE_LINK,'用例管理系統(tǒng)')
def test_with_testcase():
pass
格式3: @allure.issue(url,name),添加bug管理系統(tǒng)鏈接。
@allure.issue('66666','bug管理系統(tǒng)')
def test_with_issue():
pass
4、allure報告中添加用例分類
應(yīng)用場景:可以為項目,以及項目下的不同模塊對用例進(jìn)行分類管理。也可以運行某個類別下的用例。
報告展示:類別會展示在測試報告的Behaviors欄目下。
Allure提供了三個裝飾器:
@allure.epic:敏捷里面的概念,定義史詩,往下是feature。
import allure
@allure.epic('需求1')
class TestEpic:
def test_case1(self):
print('用例1')
def test_case2(self):
print('用例2')
def test_case3(self):
print('用例3')
@allure.epic('需求1')
class TestEpic1:
def test_case1(self):
print('用例1')
def test_case2(self):
print('用例2')
def test_case3(self):
print('用例3')
場景:希望在報告中看到測試功能,子功能或場景。
子功能上加@allure.story、@allure.feature
@allure.feature:功能點的描述,理解成模塊往下是story。
@allure.story:故事story是 feature的子集。
import allure
@allure.epic('需求1')
@allure.feature('功能模塊1')
class TestEpic:
@allure.story('子功能1')
@allure.title('用例1')
def test_case1(self):
print('用例1')
@allure.story('子功能2')
@allure.title('用例2')
def test_case2(self):
print('用例2')
@allure.story('子功能2')
@allure.title('用例3')
def test_case3(self):
print('用例3')
@allure.story('子功能1')
@allure.title('用例4')
def test_case4(self):
print('用例4')
@allure.story('子功能3')
@allure.title('用例5')
def test_case5(self):
print('用例5')
@allure.epic('需求1')
@allure.feature('功能模塊2')
class TestEpic1:
@allure.story('子功能4')
def test_case1(self):
print('用例1')
@allure.story('子功能5')
def test_case2(self):
print('用例2')
def test_case3(self):
print('用例3')
@allure.epic('需求2')
@allure.feature('功能模塊1')
class TestEpic2:
def test_case1(self):
print('用例1')
def test_case2(self):
print('用例2')
def test_case3(self):
print('用例3')
運行特定類別的用例
1、只運行epic名為”需求1“的測試用例
pytest demo1/test_allure_feature.py --alluredir=./reports --clean-all
uredir --allure-epics=需求1
2、只運行feature名為”功能模塊2“的測試用例
pytest demo1/test_allure_feature.py --alluredir=./reports --clean-all
uredir --allure-features=功能模塊2
3、只運行story名為”子功能1“的測試用例
pytest demo1/test_allure_feature.py --alluredir=./reports --clean-all
uredir --allure-stories=子功能1
4、只運行story名為”子功能2“和”子功能2“的測試用例
pytest demo1/test_allure_feature.py --alluredir=./reports --clean-all
uredir --allure-stories=子功能2,子功能3 -vs
5、運行story名為”子功能2“和 feature名”功能模塊1“的測試用例
pytest demo1/test_allure_feature.py --alluredir=./reports --clean-all
uredir --allure-stories=子功能2 --allure-features=功能模塊1
總結(jié)
epic:相當(dāng)于定義一個項目。
feature:相當(dāng)于一個功能模塊,相當(dāng)于testsuite,可以管理很多個子分支story
story:相當(dāng)于對應(yīng)這個功能或者模塊下的不同場景,分支功能。
epic 與feature、feature 與story類似于父子關(guān)系。
5、allure報告中添加用例描述
應(yīng)用場景:Allure支持往測試報告中對測試用例添加非常詳細(xì)的描述語,用來描述測試用例詳情。
Allure添加描述的四種方式:
方式一:使用裝飾器
@allure.description()傳遞一個字符串參數(shù)來描述測試用例。
import allure
@allure.description("""
多行描述信息:<br/>
通過傳遞字符串參數(shù)添加一段描述語句,</br>
使用裝飾器@allure.description
""")
def test_description_string():
assert True
方式二:使用裝飾器
@allure.description_ html傳遞一段HTML文本來描述測試用例。
@allure.description_html("""
<img class="index-logo-src" src="http://www.baidu.com/img/flexible/logo/pc/result.png" alt="到百度首頁" title="到百度首頁">
""")
def test_description_html():
assert True
方式三:直接在測試用例方法中通過編寫文檔注釋的方法來添加描述。會按照給定的格式展示,不需要添加<br/>
def test_description_desc():
"""
直接寫在測試用例中
通過編寫文檔注釋的方法
來添加描述
會按照給定的格式展示,不需要添加<br/>
:return:
"""
assert True
6、allure報告中添加用例優(yōu)先級
應(yīng)用場景:用例執(zhí)行時,希望按照嚴(yán)重級別執(zhí)行測試用例。
解決:可以為每個用例添加一個等級的裝飾器,用法: allure.severity。
Allure對嚴(yán)重級別的定義分為5個級別:
Blocker級別:中斷缺陷(客戶端程序無響應(yīng),無法執(zhí)行下一步操作)。
Critical級別:臨界缺陷(功能點缺失)。
Normal級別:普通缺陷(數(shù)值計算錯誤)。
Minor級別:次要缺陷(界面錯誤與UI需求不符)。
Trivial級別:輕微缺陷(必輸項無提示,或者提示不規(guī)范)。
使用裝飾器添加用例方法/類的級別。
類上添加的級別,對類中沒有添加級別的方法生效。
未加級別標(biāo)簽的用例,在運行時,是不會被收集上來的
但是在類中沒有加標(biāo)簽,會隨著類上加的標(biāo)簽來收集的
import allure
未加級別標(biāo)簽的用例,在運行時,是不會被收集上來的
def test_with_no_serverity_label():
pass
@allure.severity(allure.severity_level.TRIVIAL)
def test_with_trivial_serverity():
pass
@allure.severity(allure.severity_level.NORMAL)
def test_with_trivial_serverity1():
pass
對某一級別的用例運行時添加命令行參數(shù)–allure-severities:
pytest test_allure_severity.py --alluredir=./reports --clean-al
luredir --allure-severities=trivial
7、allure報告中添加用例支持tags標(biāo)簽
Allure報告支持的一些常見Pytest特性包括xfail、
skipif、fixture等。測試結(jié)果會展示特定的標(biāo)識在用例詳情頁面。
8、allure報告中添加pytest.fixture
應(yīng)用場景:fixture和finalizer是分別在測試開始之前和測試結(jié)束之后由Pytest調(diào)用的實用程序函數(shù)。Allure跟蹤每個fixture的調(diào)用,并詳細(xì)顯示調(diào)用了哪些方法以及哪些參數(shù),從而保持了調(diào)用的正確順序。
import allure
import pytest
@pytest.fixture()
def func():
print('前置操作')
yield
print('后置操作')
class TestDemo:
def test_a(self,func):
assert True
def test_b(self):
pass
五、失敗用例重試功能
Allure可以收集用例運行期間,重試的用例的結(jié)果,以及這段時間重試的歷史記錄。
安裝:
pip install pytest-rerunfailures
import pytest
import allure
@pytest.mark.flaky(reruns=2,reruns_delay=2)
class TestDemo:
def test_a(self):
assert True
def test_b(self):
assert False
六、allure報告中添加附件
1、添加圖片
應(yīng)用場景:在做UI自動化測試時,可以將頁面截圖,或者出錯的頁面進(jìn)行截圖,將截圖添加到測試報告中展示,輔助定位問題。
解決方案:使用allure.attach或者allure.attach.file()添加圖片。
語法:allure.attach.file( source,name, attachment_type,extension)
參數(shù)解釋:
source:文件路徑,相當(dāng)于傳一個文件。
name:附件名字。
attachment_type:附件類型,是
allure.attachment_type其中的一種(支持PNG、JPG、BMP、GIF等)。
extension:附件的擴(kuò)展名。
案例1
import pytest
import allure
class TestWithAttach:
def test_pic(self):
allure.attach.file(source="./1.jpeg",
name='圖片',
attachment_type=allure.attachment_type.PNG,
extension="jpeg")
案例2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/2/20 18:46
# @Author : 杜蘭特
# @File : test_allure_pic.py
import pytest
import allure
class TestWithAttach:
def test_pic(self):
allure.attach.file(source="./1.jpeg",
name='圖片',
attachment_type=allure.attachment_type.PNG,
extension="jpeg")
def test_pic1(self):
with open('./1.jpeg',mode='rb') as f:
file=f.read()
print(file)
allure.attach(file,name='頁面截圖',attachment_type=allure.attachment_type.PNG)
2、添加日志
應(yīng)用場景:報告中添加詳細(xì)的日志信息,有助于分析定位問題。
解決方案:使用python自帶的logging模塊生成日志,日志會自動添加到測試報告中。
import allure
from utils.log_util import logger
@allure.epic("需求1")
@allure.feature("功能模塊一")
class TestEpic:
@allure.story("子功能一")
@allure.title("用例1")
def test_case1(self):
logger.info("這是 TestEpic 第一條用例")
print("用例1")
@allure.story("子功能二")
@allure.title("用例2")
def test_case2(self):
logger.debug("這是 TestEpic 第二條用例")
print("用例2")
@allure.story("子功能二")
@allure.title("用例3")
def test_case3(self):
logger.warning("這是 TestEpic 第三條用例")
print("用例3")
@allure.story("子功能一")
@allure.title("用例4")
def test_case4(self):
logger.error("這是 TestEpic 第四條用例")
print("用例4")
@allure.story("子功能三")
@allure.title("用例5")
def test_case5(self):
print("用例5")
@allure.epic("需求1")
@allure.feature("功能模塊二")
class TestEpic1:
@allure.story("子功能四")
def test_case1(self):
print("用例1")
@allure.story("子功能五")
def test_case2(self):
print("用例2")
def test_case3(self):
print("用例3")
@allure.epic("需求2")
class TestEpic2:
def test_case1(self):
print("用例1")
def test_case2(self):
print("用例2")
def test_case3(self):
print("用例3")
3、添加html
應(yīng)用場景:可以定制測試報告頁面效果,可以將HTML類型的附件顯示在報告頁面上。
解決方案:使用allure.attach()添加html代碼。
語法:
allure.attach (body ,name,attachment_type,extension),參數(shù)解釋:
body:要寫入附件的內(nèi)容(HTML代碼塊)。
name:附件名字。
attachment_type:附件類型,是allure.attachment_type其中的一種。
extension:附件的擴(kuò)展名。
import allure
from utils.log_util import logger
class TestAllureHTML:
def test_html(self):
logger.info('測試日志')
allure.attach(body="""
<img class="index-logo-src" src="http://www.baidu.com/img/flexible/logo/pc/result.png" alt="到百度首頁" title="到百度首頁">
""",
name='添加html',
attachment_type=allure.attachment_type.HTML,
extension='html')
4、添加視頻
應(yīng)用場景:在做UI自動化測試時,可以將頁面截圖,或者出錯的頁面進(jìn)行截圖,將截圖添加到測試報告中展示,輔助定位問題。
解決方案:使用allure.attach. file()添加視頻。
語法:
allure.attach (body ,name,attachment_type,extension),參數(shù)解釋:
body:要寫入附件的內(nèi)容(HTML代碼塊)。
name:附件名字。
attachment_type:附件類型,是allure.attachment_type其中的一種。
extension:附件的擴(kuò)展名。
import allure
class TestWithAttach:
def test_video(self):
allure.attach.file("xxx.mp4",
name='視頻資源',
attachment_type=allure.attachment_type.MP4,
extension='mp4')
七、定制allure報告
應(yīng)用場景:針對不同的項目可能需要對測試報告展示的效果進(jìn)行定制,比如修改頁面的logo、修改項目的標(biāo)題或者添加一些定制的功能等等。文章來源:http://www.zghlxwxcb.cn/news/detail-413331.html
1、修改頁面的logo
1.修改allure.yml文件,添加logo插件custom-logo-plugin(在allure安裝路徑下,可以通過where allure或者which allure查看allure安裝路徑))。
2.編輯styles.css文件,配置logo圖片。文章來源地址http://www.zghlxwxcb.cn/news/detail-413331.html
到了這里,關(guān)于pytest測試框架——allure報告的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!