pytest合集(4)— 使用pytest-html插件生成HTML测试报告
1、pytest-html插件安裝
pytest-html 是 pytest 的一個插件,它為測試結果生成 HTML 報告。
要求: Python >=3.6 或 PyPy3。
pip install pytest-html
2、在當前目錄下生成測試報告
pytest --html=report.html
使用chrome瀏覽器打開測試報告
3、在指定路徑生成測試報告
pytest --html=./report/report.html
說明:
生成測試報告的時候,如果在目標路徑下已經存在同名的報告,原報告會被覆蓋掉。
4、創建一個獨立的報告(合并css樣式)
首先我們來看下上面的方法生成的測試報告,css和html是分開存儲的。分享報告的時候css樣式會丟失。
為了方便后期要通過郵件發送報告,可以把css樣式合并到html里,建議使用下面的方法來創建一個獨立的報告。
pytest --html=report.html --self-contained-html
?觀察可知,只生成了一份HTML文件,沒有css文件了。
5、定制化測試報告(進階)?
觀察上面的測試報告可知,默認生成的測試報告有Title、Environment、Summary、Results 加上表格,五個部分。
pytest-html 插件提供了hooks鉤子函數來幫助我們實現定制化報告,鉤子函數需要寫入conftest.py文件。
(2)conftest.py文件如下:
# content of conftest.py import pytest from datetime import datetime from py.xml import html# 編輯報告標題 def pytest_html_report_title(report):report.title = "My very own title!"# 運行測試前修改環境信息 def pytest_configure(config):config._metadata["foo"] = "bar"# # 運行測試后修改環境信息 # @pytest.hookimpl(tryfirst=True) # def pytest_sessionfinish(session, exitstatus): # session.config._metadata["foo"] = "bar"# 編輯摘要信息 def pytest_html_results_summary(prefix, summary, postfix):prefix.extend([html.p("foo: bar")])# 測試結果表格 def pytest_html_results_table_header(cells):cells.insert(1, html.th("Time", class_="sortable time", col="time"))cells.pop()def pytest_html_results_table_row(report, cells):cells.insert(1, html.td(datetime.utcnow(), class_="col-time"))cells.pop()(2)生成的測試報告如下:
6、HTML測試報告 No log output captured.
觀察html測試報告可以看到沒有任何pytest捕獲的日志信息。
?解決方案:
pytest運行的時候添加命令行參數?--capture=sys
運行結果如下:
?關于pytest捕獲日志信息詳見:
pytest合集(11)— 日志管理_篤行之.kiss的博客-CSDN博客
reference:
pytest-html — pytest-html documentation
總結
以上是生活随笔為你收集整理的pytest合集(4)— 使用pytest-html插件生成HTML测试报告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt跨平台开发编程技巧总结
- 下一篇: 多谐振荡器的LabVIEW仿真