1. 程式人生 > 其它 >Pytest 使用Allure測試報告

Pytest 使用Allure測試報告

簡介:Allure非常適合作為自動化測試報告,這裡總結下Pytest使用的Allure測試報告的用法

環境準備
所需環境
ide使用PyCharm
python 3.7
pytest 5.3.2
allure-pytest 2.8.13
allure-pytest 安裝
在已經安裝python3和pytest的前提下,開啟PyCharm,進入Project Interpreter,點選“+”,新增allure-pytest包

搜尋allure,選中allure-pytest,點選“Install Package"

allure命令列工具安裝
這個工具主要用來把測試用例的執行結果轉換成html格式
去GitHub上下載:https://github.com/allure-framework/allure2/releases

下載完成後解壓到本地,並把bin目錄新增到環境變數

設定環境變數,右擊電腦,選擇【屬性】,點選【高階系統設定】,點選【環境變數】,選擇系統變數,找到path變數,點選【編輯】,點選【新建】,輸入\安裝路徑\allure\bin(注意:這裡的路徑一定要包括 \bin,點選【確定】

檢測環境變數是否正確配置,按住鍵盤【win】+【R】,對話方塊中輸入cmd,回車進入命令列,輸入allure,如果出現以下資訊,則配置成功

程式碼演示
程式碼如下,test_allure_demo.py

# encoding: utf-8 
"""
@File    : test_allure_demo.py
@Author  : 靈樞
@Time    : 2020/4/13 5:05 PM
@Desc    :  
""" import allure @allure.step("步驟1:開啟百度") def step_1(): print("111") @allure.step("步驟2:輸入關鍵字") def step_2(): print("222") @allure.feature("搜尋") class TestEditPage(): @allure.story("百度搜索") def test_1(self): '''這是測試百度搜索''' step_1() step_2() print("
百度一下,你就知道") @allure.story("谷歌搜尋") def test_2(self): '''這是測試谷歌搜尋''' assert 1 == 2, "搜尋失敗"

在PyCharm的Terminal視窗執行:
先切換到測試程式碼的目錄下,然後執行命令:

pytest test_allure_demo.py --alluredir=output

terminal終端輸入allure serve 報告目錄(注意:pycharm有時候找不到系統的變數,以管理員身份執行Pycharm可以解決)或者cmd命令,在專案目錄下輸入allure serve 報告目錄,生成html視覺化報告。注意:報告生成後,自動呼叫預設瀏覽器顯示報告 ,有可能會呼叫IE瀏覽器,無法顯示,將地址複製後在谷歌中開啟即可。

執行後,會自動開啟瀏覽器的一個頁面來顯示測試報告,如下圖

檢視報告



test