1. 程式人生 > 實用技巧 >Pytest自動化測試 - 完美結合Allure

Pytest自動化測試 - 完美結合Allure

簡介

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>
你也可以找到該目錄,使用瀏覽器開啟該目錄下index.html。注意:有時開啟會找不到資料或者亂碼,如果你使用的是pycharm,請在pycharm中右擊開啟。 4)  如果要刪除生成的報告資料,只需執行:
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

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。