Pytest測(cè)試報(bào)告工具Allure用法介紹
簡介
Allure Framework是一種靈活的、輕量級(jí)、多語言測(cè)試報(bào)告工具。
不僅可以以簡潔的網(wǎng)絡(luò)報(bào)告形式非常簡潔地顯示已測(cè)試的內(nèi)容,
而且還允許參與開發(fā)過程的每個(gè)人從日常執(zhí)行中提取最大程度的有用信息和測(cè)試。
從開發(fā)/測(cè)試的角度來看:
Allure報(bào)告可以快速查看到缺陷點(diǎn),可以將測(cè)試未通過劃分為Bug和中斷的測(cè)試。
還可以配置日志,步驟,固件,附件,時(shí)間,歷史記錄,以及與TMS的集成和Bug跟蹤系統(tǒng),以便掌握所有信息。
從管理者的角度來看:
Allure提供了一個(gè)清晰的全局,涵蓋了所涵蓋的功能,缺陷聚集的位置,執(zhí)行時(shí)間表,以及許多其他方便的事情。
獨(dú)特的模塊化和可擴(kuò)展性,確保你能夠進(jìn)行適當(dāng)?shù)奈⒄{(diào),以使更適合你自己。
官方文檔:https://docs.qameta.io/allure/
部署使用
Pytest作為一個(gè)高擴(kuò)展性、功能強(qiáng)大的自動(dòng)化測(cè)試框架,自身的測(cè)試結(jié)果是較為簡單的,如果想要一份完整測(cè)試報(bào)告需要其他插件的支持。
如果你對(duì)測(cè)試報(bào)告要求沒那么高,你可以使用pytest-html插件,基本覆蓋了測(cè)試報(bào)告的常規(guī)內(nèi)容。
但是如果你想查看清晰的測(cè)試過程、多維度的測(cè)試報(bào)告、自定義一些輸出,以及與用例和缺陷系統(tǒng)集成等,那allure-python將是你的"不二人選"。
注意:allure-pytest從1.7之后已棄用,從2.0版本開始遷移至allure-python項(xiàng)目(即使用allure2),另外要運(yùn)行allure命令行也需要Java的支持。
1、安裝:
1)allure-pytest插件:
pip install -U allure-pytest
這將安裝allure-pytest和allure-python-commons程序包,以生成與allure2兼容的報(bào)告數(shù)據(jù)。
2)allure工具:
官方下載地址:https://github.com/allure-framework/allure2/releases
我的下載鏈接:http://xiazai.jb51.net/202207/yuanma/allure-commandline-2.13.5_jb51.rar
解壓軟件包(建議直接放到Python文件夾下),然后添加bin目錄到環(huán)境變量中,最后使用allure --version驗(yàn)證是否安裝成功。
2、基本使用
>>> 要使allure偵聽器能夠在測(cè)試執(zhí)行過程中收集結(jié)果,只需添加 --alluredir 選項(xiàng)并提供路徑即可存儲(chǔ)結(jié)果。
pytest --alluredir=<directory-with-results>
如果你運(yùn)行后進(jìn)行了用例更改,那么下次運(yùn)行可能還是會(huì)查看到之前記錄,可添加 --clean-alluredir 選項(xiàng)清除之前記錄。
pytest --alluredir=<directory-with-results> --clean-alluredir
>>> 要在測(cè)試完成后查看實(shí)際報(bào)告,你需要使用allure命令行應(yīng)用程序從結(jié)果生成報(bào)告。
1)在默認(rèn)瀏覽器中顯示生成的報(bào)告
allure serve <my-allure-results>
2)要從現(xiàn)有的Allure結(jié)果生成報(bào)告,可以使用以下命令:
allure generate <directory-with-results>
默認(rèn)報(bào)告將生成到allure-report文件夾,你可以使用 -o 標(biāo)志更改目標(biāo)文件夾:
allure generate <directory-with-results> -o <directory-with-report>
3)生成報(bào)告后,可以在默認(rèn)系統(tǒng)瀏覽器中將其打開,只需運(yùn)行:
allure open <directory-with-report>
你也可以找到該目錄,使用瀏覽器打開該目錄下index.html。注意:有時(shí)打開會(huì)找不到數(shù)據(jù)或者亂碼,如果你使用的是pycharm,請(qǐng)?jiān)趐ycharm中右擊打開。
4)如果要?jiǎng)h除生成的報(bào)告數(shù)據(jù),只需運(yùn)行:
allure report clean
默認(rèn)情況下,報(bào)告命令將在allure-results文件夾中查找報(bào)告,如果要從其他位置使用報(bào)告,則可以使用-o選項(xiàng)。
5)你也可以使用allure help命令查看更多幫助。
測(cè)試報(bào)告
你可以在allure報(bào)告中看到所有默認(rèn)的pytest狀態(tài):只有由于一個(gè)斷言錯(cuò)誤而未成功進(jìn)行的測(cè)試將被標(biāo)記為失敗,其他任何異常都將導(dǎo)致測(cè)試的狀態(tài)為壞。
示例:
# test_sample.py import pytest # 被測(cè)功能 def add(x, y): return x + y # 測(cè)試類 class TestAdd: # 跳過用例 def test_first(self): pytest.skip('跳過') assert add(3, 4) == 7 # 異常用例 def test_second(self): assert add(-3, 4) == 1 raise Exception('異常') # 成功用例 def test_three(self): assert add(3, -4) == -1 # 失敗用例 def test_four(self): assert add(-3, -4) == 7
# conftest.py import pytest @pytest.fixture(scope='session', autouse=True) def db(): print('start') yield print('closed')
運(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 sF.F [100%] =============================================================================== FAILURES ================================================================================ __________________________________________________________________________ TestAdd.test_second __________________________________________________________________________ self = <test_sample.TestAdd object at 0x000000000464F278> def test_second(self): assert add(-3, 4) == 1 > raise Exception('異常') E Exception: 異常 test_sample.py:21: Exception ___________________________________________________________________________ TestAdd.test_four ___________________________________________________________________________ self = <test_sample.TestAdd object at 0x000000000464FD30> def test_four(self): > assert add(-3, -4) == 7 E assert -7 == 7 E + where -7 = add(-3, -4) test_sample.py:29: AssertionError ======================================================================== short test summary info ======================================================================== FAILED test_sample.py::TestAdd::test_second - Exception: 異常 FAILED test_sample.py::TestAdd::test_four - assert -7 == 7 ================================================================ 2 failed, 1 passed, 1 skipped in 0.14s =================================================================
生成報(bào)告:
E:\workspace-py\Pytest>allure generate --clean report Report successfully generated to allure-report
查看目錄:
E:\workspace-py\Pytest>tree 文件夾 PATH 列表 卷序列號(hào)為 B2C1-63D6 E:. ├─.idea ├─.pytest_cache │ └─v │ └─cache ├─allure-report │ ├─data │ │ ├─attachments │ │ └─test-cases │ ├─export │ ├─history │ ├─plugins │ │ ├─behaviors │ │ ├─jira │ │ ├─junit │ │ ├─packages │ │ ├─screen-diff │ │ ├─trx │ │ ├─xctest │ │ ├─xray │ │ └─xunit-xml │ └─widgets ├─report └─__pycache__
查看報(bào)告:
Overview:總覽,顯示用例執(zhí)行情況、嚴(yán)重程度分布、環(huán)境信息等。
Categories:分類,按用例執(zhí)行結(jié)果分類,異常錯(cuò)誤和失敗錯(cuò)誤。
Suites:套件,按測(cè)試用例套件分類,目錄 ->測(cè)試文件 -> 測(cè)試類 ->測(cè)試方法。
Graphs:圖表,顯示用例執(zhí)行分布情況,狀態(tài)、嚴(yán)重程度、持續(xù)時(shí)間、持續(xù)時(shí)間趨勢(shì)、重試趨勢(shì)、類別趨勢(shì)、整體趨勢(shì)。
Timeline:時(shí)間線,顯示用例耗時(shí)情況,具體到各個(gè)時(shí)間點(diǎn)用例執(zhí)行情況
Behaviors:行為,按用例行為舉止分類(以標(biāo)記文字形式顯示,需要用例添加allure相關(guān)裝飾器)
Package:配套,按目錄形式分類,顯示不同的目錄用例執(zhí)行情況。
用例詳情:
Allure報(bào)告不僅能顯示pytest不同執(zhí)行結(jié)果狀態(tài),錯(cuò)誤情況,固件等,還能捕獲參數(shù)化測(cè)試所有參數(shù)名稱和值。
用例:
# test_sample.py import pytest import allure # 被測(cè)功能 def add(x, y): return x + y # 測(cè)試類 @allure.feature("測(cè)試練習(xí)") class TestLearning: data = [ [3, 4, 7], [-3, 4, 1], [3, -4, -1], [-3, -4, 7], ] @allure.story("測(cè)試用例") @allure.severity(allure.severity_level.NORMAL) @pytest.mark.parametrize("data", data) def test_add(self, data): assert add(data[0], data[1]) == data[2]
報(bào)告:
到此這篇關(guān)于Pytest測(cè)試報(bào)告工具Allure的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python多任務(wù)版靜態(tài)Web服務(wù)器實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Python靜態(tài)Web服務(wù)器多任務(wù)版實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06django restframework序列化字段校驗(yàn)規(guī)則
本文主要介紹了django restframework序列化字段校驗(yàn)規(guī)則,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Python+Selenium實(shí)現(xiàn)自動(dòng)化的環(huán)境搭建的步驟(圖文)
這篇文章主要介紹了Python+Selenium實(shí)現(xiàn)自動(dòng)化的環(huán)境搭建的步驟(圖文),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python利用joblib進(jìn)行并行數(shù)據(jù)處理的代碼示例
在數(shù)據(jù)量比較大的情況下,數(shù)據(jù)預(yù)處理有時(shí)候會(huì)非常耗費(fèi)時(shí)間,可以利用 joblib 中的 Parallel 和 delayed 進(jìn)行多CPU并行處理,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10Python內(nèi)置函數(shù)bin() oct()等實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
使用Python內(nèi)置函數(shù):bin()、oct()、int()、hex()可實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換;先看Python官方文檔中對(duì)這幾個(gè)內(nèi)置函數(shù)的描述,需要了解的朋友可以參考下2012-12-12基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識(shí)別
這篇文章主要為大家詳細(xì)介紹了基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Python?turtle.right與turtle.setheading的區(qū)別講述
這篇文章主要介紹了Python?turtle.right與turtle.setheading的區(qū)別,本文以turtle.right為例給大家詳細(xì)介紹,需要的朋友可以參考下2022-03-03