一、前戲 在之前我們已經學會使用 pytest-html 插件生成 html 格式的測試報告: 1 # 第一步,安裝插件 2 pip install pytest-html 3 4 # 第二步,執行用例時使用 --html 參數 5 ## main 函數中執行 6 if __name__ == ...
一、前戲
在之前我們已經學會使用 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
目錄下: