1. 程式人生 > >Pytest測試框架(五):pytest + allure生成測試報告

Pytest測試框架(五):pytest + allure生成測試報告

Allure 是一款輕量級、支援多語言的開源自動化測試報告生成框架,由Java語言開發,可以整合到 Jenkins。 pytest 測試框架支援Allure 報告生成。 pytest也可以生成junit格式的xml報告和HTML報告,命令如下: ```shell pytest test_demo.py --junitxml=report.xml pytest test_demo.py --html=report.html #需要安裝外掛:pip install pytest-html ``` Allure 報告更加靈活美觀,本文介紹如何使用pytest 生成 allure測試報告 # 環境安裝 ## 安裝allure 1. allure包下載:[https://github.com/allure-framework/allure2/releases](https://github.com/allure-framework/allure2/releases) 2. 解壓 -> 進入bin目錄 -> 執行allure.bat, 3. 把bin目錄加入PATH環境變數 ![](https://img2020.cnblogs.com/blog/2229336/202012/2229336-20201220131659099-260909699.png) allure官網 : [http://allure.qatools.ru/](http://allure.qatools.ru/) allure文件 : [https://docs.qameta.io/allure/#](https://docs.qameta.io/allure/#) ## 安裝 allure-pytest外掛 ```python pip install allure-pytest ``` # 生成Allure報告 ## 執行 ```shell pytest [測試檔案] -s -q --alluredir=./result #--alluredir用於指定儲存測試結果的路徑) ``` ## 檢視測試報告 方式一:直接開啟預設瀏覽器展示報告 `allure serve ./result/ ` 方式二:從結果生成報告 - 生成報告 `allure generate ./result/ -o ./report/ --clean` (覆蓋路徑加--clean) - 開啟報告 `allure open -h 127.0.0.1 -p 8883 ./report/` 例項程式碼:[https://docs.qameta.io/allure/#_pytest](https://docs.qameta.io/allure/#_pytest) test_allure.py: ```python import pytest def test_success(): """this test succeeds""" assert True def test_failure(): """this test fails""" assert False def test_skip(): """this test is skipped""" pytest.skip('for a reason!') def test_broken(): raise Exception('oops') ``` ### 方法1 執行測試用例: ```shell pytest test_allure.py --alluredir=./result/1 ``` ![](https://img2020.cnblogs.com/blog/2229336/202012/2229336-20201220131821093-470055224.png) 開啟報告: ```python > allure serve ./result/1 Generating report to temp directory... Report successfully generated to C:\Users\10287\AppData\Local\Temp\6968593833275403330\allure-report Starting web server... 2020-10-25 20:59:42.368:INFO::main: Logging initialized @4873ms to org.eclipse.jetty.util.log.StdErrLog Server sta