pytest(11)-Allure生成測試報告


Allure是一個開源的測試報告生成框架,提供了測試報告定制化功能,相較於我們之前使用過pytest-html插件生成的html格式的測試報告,通過Allure生成的報告更加規范、清晰、美觀。

pytest框架支持使用Allure生成測試報告,接下來讓介紹pytest怎樣結合Allure生成測試報告。

環境搭建

安裝allure-pytest

步驟1需要先安裝插件allure-pytest,可以理解為用於連接pytestallure,使它們可以結合使用。

安裝命令:pip install allure-pytest

安裝Allure

步驟2中需要安裝Allure,需要去github下載,地址為:https://github.com/allure-framework/allure2/releases

根據操作系統在最新版本中選擇對應格式的安裝文件進行下載,Windows系統選擇allure-2.xx.x.zip下載,如下圖所示:

下載后解壓文件,並bin文件所在的路徑加入系統環境變量,再重啟電腦,怎樣加入環境變量這里不啰嗦,不知道的同學可以百度。

至此,環境搭建完成。

定制報告

Allure提供了很多特性用於定制生成測試報告,腳本中加入這些特性可以對測試步驟進行詳細的說明,且不會對測試代碼邏輯產生影響。

接下來以在線購物平台的購物車功能模塊下單模塊簡單舉例說明,測試模塊test_case.py代碼如下:

import allure
import pytest
import os

@allure.step("登錄獲取token")
def get_token():
    print("請求登錄接口獲取token")

@allure.step("加入購物車")
def add_to_shopping_trolley():
    print("請求加入購物車接口")

@allure.step("查詢我的購物車")
def get_shopping_trolley_goods():
    print("請求查詢我的購物車接口")

@allure.step("清空購物車")
def empty_shopping_trolley():
    print("請求清空購物車接口")

@allure.step("下單")
def place_order():
    print("請求下單接口")


@allure.epic("xx在線購物平台接口測試")
@allure.feature("購物車功能模塊")
class TestShoppingTrolley:

    @allure.story("商品加入購物車")
    @allure.title("正向用例--將庫存數>0的商品加入購物車")
    @allure.description("校驗庫存數不為0的商品加入購物車是否正常")
    @allure.severity("critical")
    def test_add_goods(self):
        get_token()
        add_to_shopping_trolley()

    @allure.story("商品加入購物車")
    @allure.title("異常用例--將庫存數=0的商品加入購物車")
    @allure.description("校驗庫存數為0的商品加入購物車是否提示正確的錯誤信息")
    @allure.severity("normal")
    def test_add_goods_error(self):
        get_token()
        add_to_shopping_trolley()

    @allure.story("查詢購物車商品數量")
    @allure.title("查詢購物車所有商品的總數量")
    @allure.description("校驗查詢購物車所有商品的總數量是否正常")
    @allure.severity("critical")
    def test_get_goods_quantity(self):
        get_token()
        add_to_shopping_trolley()
        get_shopping_trolley_goods()

    @allure.story("查詢購物車商品數量")
    @allure.title("查詢購物車單個商品的數量")
    @allure.description("校驗查詢購物車單個商品的數量是否正常")
    @allure.severity("critical")
    def test_get_goods_quantity(self):
        get_token()
        add_to_shopping_trolley()
        get_shopping_trolley_goods()

    @allure.story("清空購物車")
    @allure.title("加入商品后再清空購物車")
    @allure.description("校驗清空購物車接口功能是否正常")
    @allure.severity("normal")
    def test_empty_shopping_trolley(self):
        get_token()
        add_to_shopping_trolley()
        empty_shopping_trolley()


@allure.epic("xx在線購物平台接口測試")
@allure.feature("下單模塊")
class TestPlaceOrder:

    @allure.story("購物車下單")
    @allure.title("商品加入購物車再下單")
    @allure.description("校驗清購物車下單功能是否正常")
    @allure.severity("critical")
    def test_place_order(self):
        get_token()
        add_to_shopping_trolley()
        place_order()

    @allure.story("立即購買下單")
    @allure.title("選擇商品不加入購物車立即購買下單")
    @allure.description("校驗立即購買下單功能是否正常")
    @allure.severity("critical")
    def test_order(self):
        get_token()
        place_order()

上面測試代碼中使用了Allure的一些特性,為了更好的理解這些特性的使用,我們可以將測試腳本由上至下進行分層:

  1. 被測系統,即被測系統的描述,如在線購物商城
  2. 功能模塊,一個被測軟件系統包含一個或多個功能模塊,如在線購物商城包含登錄、購物車、下單、支付、發貨等模塊
  3. 使用場景,一個功能模塊中包含一個或多個用戶使用場景,如購物車模塊包含加入購物車、修改數量、清空購物車的場景
  4. 測試用例,一個場景包含一條或多條測試用例,如加入購物車包含庫存數>0 或 <0等測試用例
  5. 測試步驟,一條測試用例由一個或多個測試步驟構成,如將庫存數>0商品加入購物車,測試步驟為:登錄-->商品加入購物車

對照以上分層,我們再來理解代碼中使用的這些Allure特性,如下:

  • @allure.epic(),用於描述被測軟件系統

  • @allure.feature(),用於描述被測軟件的某個功能模塊

  • @allure.story(),用於描述功能模塊下的功能點或功能場景,也即測試需求

  • @allure.title(),用於定義測試用例標題

  • @allure.description(),用於測試用例的說明描述

  • @allure.severity(),標記測試用例級別,由高到低分為 blocker、critical、normal、minor、trivial 五級

  • @pytest.allure.step(),標記通用函數使之成為測試步驟,測試方法/測試函數中調用此通用函數的地方會向報告中輸出步驟描述

生成報告

生成Allure報告步驟

pytest中Allure生成測試報告需要經過如下兩步操作:

  1. 首先,生成測試結果數據:

    # python代碼執行
    pytest.main(['testcase/test_case.py', '-s', '-q', '--alluredir', './result'])
    # 命令行形式
    pytest testcase/test_case.py --alluredir ./result
    

    即運行testcase/目錄下的測試用例,將測試結果以json文件的形式保存至當前目錄下的result文件夾中。

    參數--alluredir用於指定測試結果保存路徑。

  2. 然后,生成HTML格式的測試報告:

    # python代碼執行
    os.system('allure generate ./result -o ./report --clean')
    # 命令行形式
    allure generate ./result -o ./report --clean
    

    即將當前目錄下的result文件夾中的json數據,生成測試報告結果及index.html,並保存至當前目錄下的report文件夾中。

    --clean表示先清除之前的測試報告,使用與否視情況自行選擇。

執行代碼

因此,執行模塊run.py代碼編寫如下:

run.py

if __name__ == '__main__':
    pytest.main(['testcase/test_case.py', '-s', '-q', '--alluredir', './result'])
    os.system('allure generate ./result -o ./report --clean')

運行run.py,結果如下:

報告結果展示

運行run.py后,在run.py同級目錄下新增了result文件夾,以及文件夾下的json文件,有多少條測試用例就生成多少個名稱為xxxx-result.json的結果文件。

同樣在run.py同級目錄下新增了report文件夾,report文件夾中生成了一些文件,包括index.html,如下:

在瀏覽器中打開index.html,打開后首頁如下:

選擇點擊Behaviors后,結果如下:

Allure報告默認語言為英文,可以選擇中文,如下:

總結

可以把epic、feature、story理解為將測試用例按照功能模塊進行分類,epic為一級類目,feature為二級類目,story為三級類目。

而title、description、severity、step等則用於測試用例自身相關的描述定義。

當然,Allure還有其他的常用特性,下篇文章我們再繼續學習。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM