Python Allure庫的使用示例教程
Python Allure庫是一個開源的跨平臺的測試報告框架,用于生成漂亮、易于閱讀和易于理解的測試報告。它支持Python的標(biāo)準(zhǔn)TestRunner框架并且可以生成不同的報告格式,如HTML報告、json報告等。本文將圍繞著Python Allure庫來進行講解,讓大家了解它的用途和主要功能。
一、安裝Python Allure庫
要使用Python Allure庫,需要在命令行中安裝Allure命令行工具。
brew install allure
安裝完Allure命令行工具后,可通過pip安裝Python Allure庫。
pip install allure-pytest
二、使用Python Allure庫
1.簡單示例
import pytest
import allure
@allure.step("參數(shù)相加 : {0},{1}")
def add(x, y):
return x + y
@pytest.mark.parametrize('x', [0, 1])
@pytest.mark.parametrize('y', [2, 3])
def test_add(x, y):
with allure.step("步驟1:輸入兩個參數(shù)"):
print("輸入?yún)?shù):x->{},y->{}".format(x, y))
with allure.step("步驟2:調(diào)用相加方法"):
result = add(x, y)
with allure.step("步驟3:輸出結(jié)果"):
print("輸出結(jié)果:{}".format(result))
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/'])
import pytest
import allure
@allure.step("參數(shù)相加 : {0},{1}")
def add(x, y):
return x + y
@pytest.mark.parametrize('x', [0, 1])
@pytest.mark.parametrize('y', [2, 3])
def test_add(x, y):
with allure.step("步驟1:輸入兩個參數(shù)"):
print("輸入?yún)?shù):x->{},y->{}".format(x, y))
with allure.step("步驟2:調(diào)用相加方法"):
result = add(x, y)
with allure.step("步驟3:輸出結(jié)果"):
print("輸出結(jié)果:{}".format(result))
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/'])在腳本中首先引入pytest和allure庫,然后使用@allure.step包裝每個測試步驟,使用@allure.parametrize注釋來測試方法的步驟,最后使用pytest.main運行腳本,運行結(jié)果將生成在“./report/”文件夾中。
2.定制化報告
Python Allure庫提供了多種注釋,來增加測試報告的可讀性,讓報告更加直觀。
使用@allure.feature注釋來制定測試特性:
@allure.feature("加法運算測試")
def test_add():
pass使用@allure.story注釋來制定測試場景:
@allure.story("測試加法")
def test_add():
pass3.報告展示
在生成報告后,可以通過運行命令在瀏覽器中查看報告。
allure serve 報告目錄
三、總結(jié)
Python Allure庫是一個實用可靠的測試報告框架,它幾乎可以與Python的其他庫和框架無縫集成。利用Python Allure庫,可以輕松生成易于閱讀的測試報告,讓測試變得更加簡單便捷。
到此這篇關(guān)于Python Allure庫的使用的文章就介紹到這了,更多相關(guān)Python Allure庫使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python機器學(xué)習(xí)工具scikit-learn的使用筆記
這篇文章主要介紹了Python機器學(xué)習(xí)工具scikit-learn的使用筆記,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
python opencv旋轉(zhuǎn)圖像(保持圖像不被裁減)
這篇文章主要為大家詳細介紹了python opencv旋轉(zhuǎn)圖像,保持圖像不被裁減,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
OpenCV實現(xiàn)單目攝像頭對圖像目標(biāo)測距
這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)單目攝像頭對圖像目標(biāo)測距,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
聊聊PyTorch中eval和no_grad的關(guān)系
這篇文章主要介紹了聊聊PyTorch中eval和no_grad的關(guān)系,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
基于python SMTP實現(xiàn)自動發(fā)送郵件教程解析
這篇文章主要介紹了基于python實現(xiàn)自動發(fā)送郵件教程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python?RawString與open文件的newline換行符遇坑解決
這篇文章主要為大家介紹了Python?RawString與open文件的newline換行符遇坑解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

