pytest-html
# pytest插件生成一個HTML測試報告。 pip install pytest-html
使用
# 添加命令行參數 --html=report.html 和 --html=report.html --self-contained-html if __name__ == "__main__": pytest.main(["test_debug.py", # 測試用例 "--html=../report/report0414.html", # 生成測試報告 生成assert存放的css文件和html文件 "--self-contained-html", # 把css樣式合並到html里 僅生成html文件
])
# 運行中的日志顯示ASCII顏色 if __name__ == "__main__": pytest.main(["test_debug.py", # 測試用例 "-v", # -v 顯示詳細信息, -q 不輸出環境信息, -s 顯示程序中的打印和日志 "--html=../report/report0414.html", # 生成測試報告 "--self-contained-html", # 把css樣式合並到html里 "--color=yes", # pytest 寫入輸出顏色 ])
Allure
手動安裝
配置環境變量
解壓后將bin目錄添加到path 並執行目錄下 allure.bat 文件
pytest需要安裝插件
# 安裝依賴包 pip install allure-pytest
使用
if __name__ == "__main__": pytest.main(["test.py::Test", # "運行文件名"::"類名"::"方法名" "--alluredir", # 創建allure報告的路徑 "../report/allure/temp_jsonreport", # allure生成的報告是json格式的,需要再轉化成html格式的,所以會自動生成一個temp_jsonreport文件 "--clean-alluredir", # 先清空再生成新的測試報告 "--allure-no-capture", # 不添加pytest捕捉的logging/stdout/stderr到測試報告中 "-v", # 日志打印 -q僅報告 -s僅控制台, -v 報告及控制台 "--color=yes", # pytest 寫入輸出顏色 "-n 10", # 不使用則注釋掉,xdist多進程並發 -n后+num '-W', 'ignore:Module already imported:pytest.PytestWarning', # 忽略錯誤 # "-p", "no:cacheprovider" # 禁用插件,緩存是通過cacheprovider插件 ])
# -o是執行 --clean是清除之前生成的報告 os.system(f"allure generate ../report/allure/temp_jsonreport -o ../report/allure/html --clean") # 自動在瀏覽器中打開allure測試報告 os.system(f"allure open {os.path.join(os.path.abspath('..'), 'report/allure/html')}")
<Tips:
--clean-alluredir:如果已經存在報告,那就先清空,然后再生成新的測試報告;
--allure-no-capture:不添加pytest捕捉的logging/stdout/stderr到測試報告中;
>
allure報告結構
常用特性
使用方法 | 參數值 | 參數說明 |
---|---|---|
@allure.epic() | epic描述 | 敏捷里面的概念,定義史詩,往下是feature |
@allure.feature() | 模塊名稱 | 功能點的描述,往下是story |
@allure.story() | 用戶故事 | 用戶故事,往下是title |
@allure.title(用例的標題) | 用例的標題 | 重命名html報告名稱 |
@allure.step() | 操作步驟 | 測試用例的步驟 |
@allure.testcase() | 測試用例的鏈接地址 | 對應功能測試用例系統里面的case |
@allure.issue() | 缺陷 | 對應缺陷管理系統里面的鏈接 |
@allure.description() | 用例描述 | 測試用例的描述 |
@allure.severity() | 用例等級 | blocker,critical,normal,minor,trivial |
@allure.link() | 鏈接 | 定義一個鏈接,在測試報告展現 |
@allure.attachment() | 附件 | 報告添加附件 |
@allure.epic("模塊") #敏捷里面的概念,定義史詩,相當於module級的標簽 @allure.feature("功能") #功能點的描述,可以理解成模塊,相當於class級的標簽 @allure.story("場景") #故事,可以理解為場景,相當於method級的標簽

# -*- coding: UTF-8 -*- import sys import os import json import time import allure @allure.epic("測試腳本") @allure.feature("多線程測試") class TestPressure: """測試用例模塊""" @allure.story("測試用例") def test_01_demo(self): logs.info(datetime.datetime.now()) logs.info(datetime.datetime.now()) @allure.story("測試用例") @allure.feature("跳過測試") @pytest.mark.skip def test_03_demo(self): logs.info(datetime.datetime.now()) logs.info(datetime.datetime.now()) @allure.story("測試用例") def test_05_demo(self): logs.info(datetime.datetime.now()) assert 1 == 2, "不相等" def test_07demo(self): logs.info(datetime.datetime.now()) assert 1 == 1, "不相等" if __name__ == "__main__": pytest.main(["test_pressure.py::TestPressure", # "運行文件名"::"類名"::"方法名" "--alluredir", # 創建allure報告的路徑 "../report/allure/temp_jsonreport", # allure生成的報告是json格式的,需要再轉化成html格式的,所以會自動生成一個temp_jsonreport文件 "-v", # 日志打印 -q僅報告 -s僅控制台, -v 報告及控制台 "--color=yes", # pytest 寫入輸出顏色 # "-n 10", # 不使用則注釋掉,xdist多進程並發 -n后+num '-W', 'ignore:Module already imported:pytest.PytestWarning', # 忽略錯誤 # "-p", "no:cacheprovider" # 禁用插件,緩存是通過cacheprovider插件 ]) # -o是執行 --clean是清除之前生成的報告 os.system(f"allure generate ../report/allure/temp_jsonreport -o ../report/allure/html --clean") # 自動在瀏覽器中打開allure測試報告 os.system(f"allure open {os.path.join(os.path.abspath('..'), 'report/allure/html')}")
執行結果
指定標簽運行
--allure-epics --allure-features --allure-stories
指定運行方式
"--allure-features=多線程測試", "--allure-features 多線程測試, 跳過測試",
運行結果
遇到的問題
1、在py文件中執行執行測試用例時,未生成測試報告
if __name__ == "__main__": pytest.main(["test_debug.py", # 測試用例 "-q", # -v 顯示詳細信息, -q 不輸出環境信息, -s 顯示程序中的打印和日志 "--html=../report/report0414.html", # 生成測試報告 "--self-contained-html", # 把css樣式合並到html里 ])
方法一:
原因:pytest沒有main函數,會根據目錄執行復核pytest規則的測試用例
解決:將pycharm 的測試運行程序改為 unittest
2、使用 loguru 插件生成的日志,在生成html報告時 中文亂碼
解決方案:將loguru 日志控制台輸出關閉並添加logging輸出到控制台
完整代碼參考: https://www.cnblogs.com/phoenixy/p/16150119.html
# 刪除以前添加的處理程序並停止向其接收器發送日志。 logs.remove(handler_id=None) # 清除之前的設置 # 集成loguru到控制台(即html報告) class PropogateHandler(logging.Handler): def emit(self, record): logging.getLogger(record.name).handle(record) logs.add(PropogateHandler(), format="{time:YYYY-MM-DD HH:mm:ss} | {message}")
3、allure 目錄文件打開頁面展示Loading和404
問題原因:allure生成的html測試報告無法直接在瀏覽器打開
解決方法:
在report目錄使用命令 allure open 或allure serve
報告子目錄執行
allure open html allure serve html # 信息不完整
報告目錄執行
allure open allure\html allure serve allure\html # 信息不完整