簡介
Allure Framework是一種靈活的、輕量級、多語言測試報告工具。
不僅可以以簡潔的網絡報告形式非常簡潔地顯示已測試的內容,
而且還允許參與開發過程的每個人從日常執行中提取最大程度的有用信息和測試。
從開發/測試的角度來看:
Allure報告可以快速查看到缺陷點,可以將測試未通過划分為Bug和中斷的測試。
還可以配置日志,步驟,固件,附件,時間,歷史記錄,以及與TMS的集成和Bug跟蹤系統,以便掌握所有信息。
從管理者的角度來看:
Allure提供了一個清晰的全局,涵蓋了所涵蓋的功能,缺陷聚集的位置,執行時間表,以及許多其他方便的事情。
獨特的模塊化和可擴展性,確保你能夠進行適當的微調,以使更適合你自己。
官方文檔:https://docs.qameta.io/allure/#_pytest
部署使用
Pytest作為一個高擴展性、功能強大的自動化測試框架,自身的測試結果是較為簡單的,如果想要一份完整測試報告需要其他插件的支持。
如果你對測試報告要求沒那么高,你可以使用 pytest-html 插件,基本覆蓋了測試報告的常規內容。
但是如果你想查看清晰的測試過程、多維度的測試報告、自定義一些輸出,以及與用例和缺陷系統集成等,那 allure-python 將是你的"不二人選"。
注意:allure-pytest 從1.7之后已棄用,從2.0版本開始遷移至 allure-python 項目(即使用allure2),另外要運行allure命令行也需要Java的支持。
1、安裝:
1) allure-pytest插件:
pip install -U allure-pytest
這將安裝allure-pytest和allure-python-commons程序包,以生成與allure2兼容的報告數據。
2) allure工具:
官方下載地址:https://github.com/allure-framework/allure2/releases
我的下載鏈接:https://pan.baidu.com/s/1aCUyGoSNB8dqBEQIlvysRg 提取碼: gue5
解壓軟件包(建議直接放到Python文件夾下),然后添加bin目錄到環境變量中,最后使用 allure --version 驗證是否安裝成功。
2、基本使用
>>> 要使allure偵聽器能夠在測試執行過程中收集結果,只需添加 --alluredir 選項並提供路徑即可存儲結果。
pytest --alluredir=<directory-with-results>
如果你運行后進行了用例更改,那么下次運行可能還是會查看到之前記錄,可添加 --clean-alluredir 選項清除之前記錄。
pytest --alluredir=<directory-with-results> --clean-alluredir
>>> 要在測試完成后查看實際報告,你需要使用allure命令行應用程序從結果生成報告。
1) 在默認瀏覽器中顯示生成的報告
allure serve <my-allure-results>
2) 要從現有的Allure結果生成報告,可以使用以下命令:
allure generate <directory-with-results>
默認報告將生成到allure-report文件夾,你可以使用 -o 標志更改目標文件夾:
allure generate <directory-with-results> -o <directory-with-report>
3) 生成報告后,可以在默認系統瀏覽器中將其打開,只需運行:
allure open <directory-with-report>

allure report clean
默認情況下,報告命令將在 allure-results 文件夾中查找報告,如果要從其他位置使用報告,則可以使用 -o 選項。
5) 你也可以使用 allure help 命令查看更多幫助。
測試報告
你可以在allure報告中看到所有默認的pytest狀態:只有由於一個斷言錯誤而未成功進行的測試將被標記為失敗,其他任何異常都將導致測試的狀態為壞。
示例:
# test_sample.py import pytest # 被測功能 def add(x, y): return x + y # 測試類 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')
運行:
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 =================================================================
生成報告:
E:\workspace-py\Pytest>allure generate --clean report
Report successfully generated to allure-report
查看目錄:
E:\workspace-py\Pytest>tree 文件夾 PATH 列表 卷序列號為 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__
查看報告:
Overview:總覽,顯示用例執行情況、嚴重程度分布、環境信息等。
Categories:分類,按用例執行結果分類,異常錯誤和失敗錯誤。
Suites:套件,按測試用例套件分類,目錄 ->測試文件 -> 測試類 ->測試方法。
Graphs:圖表,顯示用例執行分布情況,狀態、嚴重程度、持續時間、持續時間趨勢、重試趨勢、類別趨勢、整體趨勢。
Timeline:時間線,顯示用例耗時情況,具體到各個時間點用例執行情況
Behaviors:行為,按用例行為舉止分類(以標記文字形式顯示,需要用例添加allure相關裝飾器)
Package:配套,按目錄形式分類,顯示不同的目錄用例執行情況。
用例詳情:
Allure報告不僅能顯示pytest不同執行結果狀態,錯誤情況,固件等,還能捕獲參數化測試所有參數名稱和值。
用例:
# test_sample.py import pytest import allure # 被測功能 def add(x, y): return x + y # 測試類 @allure.feature("測試練習") class TestLearning: data = [ [3, 4, 7], [-3, 4, 1], [3, -4, -1], [-3, -4, 7], ] @allure.story("測試用例") @allure.severity(allure.severity_level.NORMAL) @pytest.mark.parametrize("data", data) def test_add(self, data): assert add(data[0], data[1]) == data[2]
報告:
作者:Leozhanggg
出處:https://www.cnblogs.com/leozhanggg/p/14049410.html
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。