pytest生成簡單自定義測試結(jié)果的html報告
簡介
pytest-HTML 是一個插件,pytest 用于生成測試結(jié)果的 HTML 報告。
生成報告
先簡單寫個例子生成報告看看。

生成報告效果如下

此次主要是針對 Environment 和 Results 兩部分進行修改配置,讓列表部分展示的數(shù)據(jù)更清晰,增加和刪減列數(shù)據(jù)。
修改報告
這里先介紹下 conftest.py 文件,主要作用如下:
- 1 .存放你的 fixture 函數(shù)
- 2.在里面寫自己的本地插件
比如公共用例前置和后置部分,數(shù)據(jù)清理都可以放在該文件里執(zhí)行。
修改 Environment
主要分為增加配置或刪除配置:
def pytest_configure(config):
# 添加配置
config._metadata["項目名稱"] = "測試報告"
# 刪除配置
config._metadata.pop("JAVA_HOME")
config._metadata.pop("Plugins")
config._metadata.pop("Packages")
config._metadata.pop("Platform")
修改 Results
從上面生成的報告列表中,看到主要分為下面幾列數(shù)據(jù):Result、Test、Links、Duration。這里面的數(shù)據(jù)其實可以看出都沒有包含我們的測試數(shù)據(jù),無法直觀看出輸入、輸出結(jié)果。
做如下優(yōu)化:
- 1.刪除 Test、Links 列
- 2.增加幾列分別展示參數(shù)化中的內(nèi)容,如用例編號、輸入、輸出
- 3.修改用例執(zhí)行結(jié)果 show details 中內(nèi)容,自定義展示內(nèi)容
基于上述需求,要在報告中添加我們自己的測試數(shù)據(jù)展示,故需要添加一個全局變量在一個 case 執(zhí)行過程中進行記錄供調(diào)用。
創(chuàng)建全局變量:
# 定義一個全局變量,用于存儲內(nèi)容
global_data = {}
@pytest.fixture(scope="function")
def set_global_data():
"""
設(shè)置全局變量,用于關(guān)聯(lián)參數(shù)
:return:
"""
def _set_global_data(key, value):
global_data[key] = value
yield _set_global_data
global_data.clear()
修改我們的用例函數(shù),將測試數(shù)據(jù)加入到全局變量中。
@user2ize("data", case_list)
def test_case(data, set_global_data):
set_global_data("id", data.get("id"))
set_global_data("method", data.get("method"))
set_global_data("case_input", data.get("case_input"))
set_global_data("case_output", data.get("case_output"))
try:
assert data.get("case_input") == data.get("case_output")
except AssertionError:
set_global_data("error_step", "斷言失敗")
raise
conftest.py 文件中增加和刪除列。
@user3hook
def pytest_html_results_table_header(cells):
""" 更改表頭信息
:param cells:
:return:
"""
cells.insert(1, html.th('用例ID', class_="sortable", col="id"))
cells.insert(2, html.th('方法', class_="sortable", col="method"))
cells.insert(3, html.th('輸入', class_="sortable", col="case_input"))
cells.insert(4, html.th('輸出', class_="sortable", col="case_output"))
cells.pop(-1) # 刪除link
cells.pop(-2) # 刪除Test
@user4hook
def pytest_html_results_table_row(cells):
"""更改表中數(shù)據(jù)信息"""
cells.insert(1, html.td(global_data.get("id")))
cells.insert(2, html.td(global_data.get("method")))
cells.insert(3, html.td(global_data.get("case_input")))
cells.insert(4, html.td(global_data.get("case_output")))
cells.pop(-1) # 刪除link
cells.pop(-2) # 刪除Test
conftest.py 文件中修改執(zhí)行結(jié)果 show details 內(nèi)容。
@user5hook
def pytest_html_results_table_html(report, data):
if report.failed:
del data[:]
data.append(html.span(f"失敗步驟:{global_data.get('error_step')}\n輸出結(jié)果:{global_data.get('case_output')}",
class_='fail log'))
elif report.passed:
del data[:]
data.append(html.div(f"輸出結(jié)果:{global_data.get('case_output')}", class_='success log'))
生成效果報告

可以看到現(xiàn)在生成的報告內(nèi)容就可以清晰看到測試數(shù)據(jù),和我們的用例數(shù)據(jù)關(guān)聯(lián)上了。
后記
當(dāng)前只是簡單的對報告展示的數(shù)據(jù)進行了更改,感興趣可以查看官方文檔學(xué)習(xí)
https://docs.pytest.org/en/latest/reference/reference.html#hooks
以上就是pytest生成簡單自定義測試結(jié)果html報告的詳細內(nèi)容,更多關(guān)于pytest生成自定義測試html的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中的os.mkdir和os.makedirs的使用區(qū)別及如何查看某個模塊中的某些字母開頭的屬性方法
這篇文章主要介紹了python中的os.mkdir和os.makedirs的使用區(qū)別及如何查看某個模塊中的某些字母開頭的屬性方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
解決python3.x安裝numpy成功但import出錯的問題
這篇文章主要介紹了解決python3.x安裝numpy成功但import出錯的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
用Python?Tkinter庫GUI編程創(chuàng)建圖形用戶界面
這篇文章主要為大家介紹了用Python?Tkinter庫GUI編程創(chuàng)建圖形用戶界面,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
Python中pandas的dataframe過濾數(shù)據(jù)方法
這篇文章主要介紹了Python中pandas的dataframe過濾數(shù)據(jù)方法,Pandas是另外一個用于處理高級數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析的Python庫,Pandas是基于Numpy構(gòu)建的一種工具,需要的朋友可以參考下2023-07-07

