一、前戲
在之前我們已經學會使用 pytest-html 插件生成 html 格式的測驗報告:
1 # 第一步,安裝插件 2 pip install pytest-html 3 ? 4 # 第二步,執行用例時使用 --html 引數 5 ## main 函式中執行 6 if __name__ == '__main__': 7 pytest.main(["-vs", "--html=./report/result.html"]) 8 9 ## 使用命令模式執行 10 pytest -vs --html ./report/result.html

很明顯報告的效果配不上我們高大上的逼格.......除了 pytest-html 插件,pytest 還可以和 allure 結合,生成更加詳細美觀的測驗報告,
二、allure的使用
-
第一步,下載第三方插件
pip install allure-pytest
-
第二步,訪問
allure官網,下載最新的版本
網址:https://github.com/allure-framework/allure2/releases

-
第三步,解壓并配置環境變數,將解壓后的一直到
bin目錄的檔案路徑添加到計算機環境變數的path中

-
第四步,驗證是否配置成功(如果 IDE 的終端中無法執行檢查版本的命令,重啟 IDE 即可)


-
第五步,在專案的組態檔
pytest.ini中添加引數--alluredir ./tmp
1 [pytest] 2 addopts = -vs --alluredir ./tmp 3 testpaths = . 4 python_files = test_*.py 5 python_classes = Test* 6 python_functions = test
這個引數的作用是在用例執行時,會在臨時檔案夾 tmp 中生成很多 json 檔案,這些檔案記錄了用例執行程序中的相關資訊,最后生成報告使用到的資料就是從 json 中獲取的,

-
第六步,執行完用例后運行命令生成報告
allure generate ./tmp -o ./report --clean
# 引數詳情
# ./tmp:存放臨時 json 資料的目錄
# -o:表示輸出 output
# ./report:測驗報告存放目錄
# --clean:清空 report 目錄中原有的資料
為了方便起見,我們一般會把生成報告的命令直接寫在主函式里面:
1 # 根目錄下新建一個 all.py 2 import pytest 3 import os 4 ? 5 if __name__ == '__main__': 6 pytest.main() # 執行專案中所有用例 7 os.system("allure generate ./tmp -o ./report --clean") # 生成allure測驗報告
最后報告會生成在 report 目錄下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/545405.html
標籤:Python
上一篇:Pytest 韌體
下一篇:Integer使用==比較的問題
