pytest+allure生成測試報告


前言

Allure Framework是一種靈活的輕量級多語言測試報告工具,不僅可以以簡潔的網絡報告形式非常簡潔地顯示已測試的內容,而且還允許參與開發過程的每個人從日常執行中提取最大程度的有用信息。

安裝

使用pip安裝pytest和allure-pytest,加上--index-url地址,下載會快一些

pip install pytest==4.5.0 --index-url https://pypi.douban.com/simple
pip install allure-pytest==2.8.6 --index-url https://pypi.douban.com/simple

allure是一個命令行工具,需要去github上下載最新版 https://github.com/allure-framework/allure2/releases

下載完成之后,解壓到本地電腦

把bin目錄添加到環境變量Path下

使用說明

@allure.feature # 用於定義被測試的功能,被測產品的需求點
@allure.story # 用於定義被測功能的用戶場景,即子功能點
with allure.step # 用於將一個測試用例,分成幾個步驟在報告中輸出
allure.attach # 用於向測試報告中輸入一些附加的信息,通常是一些測試數據信息
@pytest.allure.step # 用於將一些通用的函數作為測試步驟輸出到報告,調用此函數的地方會向報告中輸出步驟

示例

import allure
import pytest

@allure.step("步驟1:點xxx")
def step_1():
    print("111")

@allure.step("步驟2:點xxx")
def step_2():
    print("222")

@allure.feature("編輯頁面")
class TestEditPage():
    '''編輯頁面'''

    @allure.story("這是一個xxx的用例")
    def test_1(self, login):
        '''用例描述:先登錄,再去執行xxx'''
        step_1()
        step_2()
        print("xxx")


    @allure.story("打開a頁面")
    def test_2(self, login):
        '''用例描述:先登錄,再去執行yyy'''
        print("yyy")

cd到test_allure_demo.py所在的目錄文件,命令行執行

pytest --alluredir ./report/allure_raw

執行完成后,在當前目錄下生成report目錄,report目錄下生成一個allure_raw的目錄,這里存放了測試報告的JSON格式原始文件,不能打開成html的報告

啟動allure報告服務,輸入生成的IP地址和端口即可訪問

allure serve report/allure_raw


Jenkins + allure生成報告

安裝插件


安裝后選擇重啟
安裝完成后打開jenkins首頁-系統管理-全局工具配置-Allure Commandline, 安裝 allure 命令行工具

構建運行命令

cd C:\Users\Administrator\Desktop\learn\py23\chongshi\pycs
pytest C:\Users\Administrator\Desktop\learn\py23\chongshi\pycs\test_allure_demo.py --alluredir ${WORKSPACE}/allure_raw/



免責聲明!

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



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