前言
廣深小龍最近有寫 pytest + request + yaml + allure...的測試框架,認為總體還不錯。pytest 單元測試框架需配合 allure 生成漂亮的測試報告。
一、實時生成 allure 報告
1.首先需要去github上下載最新版進行安裝:https://github.com/allure-framework/allure2/releases
2.allure 生成報告前提需安裝好 pytest 與 allure-pytest :pip install pytest allure-pytest
pip install pytest allure-pytest
3.--alluredir 指定生成的json文件存放目錄,如:pytest --alluredir ../report/allure_raw
4.實時生成報告命令:allure serve json文件目錄,運行命令后自動彈出web瀏覽器查看報告(右下角可以切換網頁語言),如下:
G:\demo\pytest_demo\api_package\cases>allure serve ../report/allure_raw Generating report to temp directory... Report successfully generated to C:\Users\ADMINI~1\AppData\Local\Temp\7463144076250537920\allure-report Starting web server... 2020-05-10 10:18:14.573:INFO::main: Logging initialized @3810ms to org.eclipse.jetty.util.log.StdErrLog Server started at <http://169.254.16.95:7885/>. Press <Ctrl+C> to exit
為什么我的allure報告會沒有數據?
顯然是 allure serve 生成json文件目錄不對應,所以要檢查下目錄是否正確。
二、生成 html 報告文件
1.命令:allure generate json文件目錄 html生成目錄 (html目錄沒有會自動生成)
G:\demo\pytest_demo\api_package\cases>allure generate ../report/allure_raw -o ../report/html/
Report successfully generated to ..\report\html
2.生成html后,右鍵 index.html 文件打開 Open in Browser 選擇一個瀏覽器打開。
3.你會發現我直接在桌面用一般瀏覽器包括 Chrom 打開顯示沒有數據,顯示Loading...
這是因為 html 導入css、js是相對路徑瀏覽器識別不出來。
最后解決思路(linux上持續集成,推薦第3):
- 1.使用:firefox 瀏覽器直接打開(此瀏覽器能識別整個html文件)
- 2.Pycharm 中 Open in Browser
- 3.部署一個小web項目,訪問一個鏈接就能看到
注:如果 allure 找不到命令,就需要檢查下環境變量是否正常。
三、簡單的 pytest html報告
1.安裝:pip install pytest-html
2.運行:pytest --html=report/report.html --self-contained-html
--html:指定報告目錄名稱
--self-contained-html:可以直接打開顯示(不會像allure顯示Loading...)
對比 unittest 不需要封裝一個runner all cases
G:\demo\pytest_demo\api_package>pytest cases --html=report/report.html --self-contained-html ================================================= test session starts ================================================= platform win32 -- Python 3.6.5, pytest-5.4.2, py-1.8.0, pluggy-0.13.1 rootdir: G:\demo\pytest_demo\api_package plugins: allure-pytest-2.8.13, html-2.1.1, metadata-1.9.0 collected 7 items cases\test_home.py ... [ 42%] cases\test_login.py .... [100%] ------------------- generated html file: file://G:\demo\pytest_demo\api_package\report\report.html -------------------- ================================================== 7 passed in 1.74s ==================================================
歡迎來大家QQ交流群一起學習:482713805