HTTPrunner 集成了 pytest,所以 HTTPrunner v3.x 可以使用 pytest 的所有插件,包括測試報告插件,例如pytest-html
和alluer-pytest
。
HTML 測試報告
HTTPrunner 安裝之後自帶 pytest-html
插件,當你想生成 HTML 測試報告時,可以添加命令參數--html
。
$ hrun /path/to/testcase --html=report.html
--html=report.html
中的report.html
是測試報告的存放路徑,沒有帶文件夾的時候會存放在命令運行的文件夾。
通過--html
生成的 HTML 報告包含了多個文件夾,如果你要創建一個單獨的 HTML 文件,可以天劍另一個命令參數--self-contained-html
。
$ hrun /path/to/testcase --html=report.html --self-contained-html
你可以參考pytest-html,獲取更多關於 pytest-html 測試報告的信息。
allure report
pytest 支持大名鼎鼎的 allure 測試報告,HTTPrunner 集成了 pytest,也天然支持 allure。
不過 HTTPrunner 默認並未安裝 allure,你需要另外安裝。
安裝有兩種方式:
- 安裝 allure 的 pytest 依賴庫
allure-pytest
; - 安裝 HTTPrunner 的 allure 依賴庫
httprunner[allure]
。
安裝 allure-pytest
:
$ pip3 install "allure-pytest"
安裝 httprunner[allure]
(推薦)
pip3 install "httprunner[allure]"
一旦 allure-pytest 準備好,以下參數就可以與 hrun/pytest 命令一起使用:
--alluredir=DIR
: 生成 allure 報告到指定目錄
--clean-alluredir
: 如果指定目錄已存在則清理該文件夾
--allure-no-capture
:不要將 pytest 捕獲的日誌記錄(logging)、標準輸出(stdout)、標準錯誤(stderr)附加到報告中
要使 Allure 偵聽器能夠在測試執行期間收集結果,只需添加--alluredir
選項,並提供指向存儲結果的文件夾路徑。如:
$ hrun /path/to/testcase --alluredir=/tmp/my_allure_results
/tmp/my_allure_results 只會存儲收集的測試結果(你將會看到一堆莫名其妙的文件,這些都是收集的測試結果),並非完成的報告,還需要通過命令生成。
要在測試完成後查看實際報告,您需要使用Allure命令行實用程序從結果中生成報告。
$ allure serve /tmp/my_allure_results
此命令將在默認瀏覽器中顯示你生成的報告。
同時你也可以在 Jenkins 中安裝 allure 報告插件,將結果與 Jenkins 集成。
你可以在 allure-pytest 查看關於 allure 報告的更多信息。