目錄
- 1、前言
- 2、pytest-html生成測驗報告
- (1)pytest-html插件安裝
- (2)pytest-html的使用
- (3)報告獨立顯示
- 3、Allure框架生成測驗報告
- (1)說明
- (2)環境準備
- (3)執行測驗并生成測驗報告
1、前言
在pytest中常用的測驗報告生成方法有兩種,一種是通過pytest-html插件來完成,另一種通過Allure框架來實作,
2、pytest-html生成測驗報告
(1)pytest-html插件安裝
pip install pytest-html
(2)pytest-html的使用
- 命令列執行:
pytest -s -v xxx.py --html=./report/report.html
main()函式執行:pytest.main(['-vs','--html=./report/report.html'])
- 全域配置:
- 在
pytest.ini的addopts屬性后面添加--html=../report/report.html

- 在
(3)報告獨立顯示
在(2)中的執行方法生成的報告,css是獨立的,分享報告的時候樣式會丟失,為了更好的分享測驗報告,可以通過添加 --self-contained-html引數將css樣式合并到html里,
pytest -v -s xxx.py --html=./report/report.html --self-contained-html
更多功能查看官方檔案:https://pytest-html.readthedocs.io/en/latest/
3、Allure框架生成測驗報告
(1)說明
Allure框架生成的報告相比pytest-html更加完美,推薦使用Allure框架來生成測驗報告,
Allure是一個Report框架,是靈活,輕量級,支持多語言的測驗報告工具,還可以集成到Jenkins上,
(2)環境準備
步驟1:安裝Allure框架
下載地址:https://github.com/allure-framework/allure2/releases
【windows】下:
1)解壓下載的框架檔案,放到自己指定的目錄中
2)把框架的bin目錄加入到Path環境變數中
3)驗證框架是否安裝成功,命令allure --version
【mac】下:
1)確定解壓下載的框架檔案,放到自己指定的目錄中
2)添加環境變數:
查看mac系統版本,我的mac版本是Bigsur,可以通過sudo nano ~/.zshrc進行配置
Apple官方說明:

PATH="/Users/qishuai/allure-2.19.0/bin:${PATH}"
export PATH`

3)驗證框架是否安裝成功,命令:allure --version,正常出現版本號即為安裝成功,但我的出現了如下提示,需要安裝jdk

mac安裝jdk參考:https://www.jianshu.com/p/199cd1abd570
安裝jdk完畢后,再次驗證框架是否安裝成功

步驟2:下載allure_pytest庫
pip install allure-pytest
至此,Allure的環境準備作業就完畢了,
(3)執行測驗并生成測驗報告
步驟1:執行并指定測驗資料存放位置
- 命令列執行:
- 在命令列中增加
--alluredir ../report/temp_report_data - eg:
pytest -s -v ./xxx.py --alluredir ../report/temp_report_data
- 在命令列中增加
- 全域組態檔:
- 在
pytest.ini檔案的addopts中添加--alluredir ../report/temp_report_data - eg:
addopts = -s -v --alluredir ../report/temp_report_data
- 在
main函式執行:pytest.main(['-v','-s','--allured','../report/temp_report_data'])
注意:不指定的話會自動在當前目錄創建一個allure_report目錄
步驟2:根據測驗資料生成html報告
通過allure框架把json格式的測驗資料轉換為測驗報告
方法1:生成報告檔案
allure generate ./report/temp_report_data -o ./report/html --clean
命令解釋:
allure generate:生成命令./report/temp_report_data:存放測驗資料的目錄-o:輸出./report/html:存放生成html報告的路徑--clean:清空./report/html路徑目錄中原來的測驗報告
補充:可通過命令創建
web服務打開創建的報告檔案
allure open -h 127.0.0.1 -p 5555 ./report/html
方法2:直接啟動web服務打開報告(不生成報告檔案)
在上面方法1中生成html測驗報告還要手動打開或輸入命令創建web服務打開,我們還可以在生成測驗資料之后直接用allure serve .report/temp_report_data命令創建一個web服務,自動在瀏覽器中打開測驗報告

參考
https://developer.aliyun.com/article/948360
https://www.cnblogs.com/qican/p/14573869.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/502967.html
標籤:Python
上一篇:pip下載慢問題解決方案
下一篇:cpu詳解
