pytest生成簡(jiǎn)單自定義測(cè)試結(jié)果的html報(bào)告
簡(jiǎn)介
pytest-HTML 是一個(gè)插件,pytest 用于生成測(cè)試結(jié)果的 HTML 報(bào)告。
生成報(bào)告
先簡(jiǎn)單寫(xiě)個(gè)例子生成報(bào)告看看。
生成報(bào)告效果如下
此次主要是針對(duì) Environment 和 Results 兩部分進(jìn)行修改配置,讓列表部分展示的數(shù)據(jù)更清晰,增加和刪減列數(shù)據(jù)。
修改報(bào)告
這里先介紹下 conftest.py 文件,主要作用如下:
- 1 .存放你的 fixture 函數(shù)
- 2.在里面寫(xiě)自己的本地插件
比如公共用例前置和后置部分,數(shù)據(jù)清理都可以放在該文件里執(zhí)行。
修改 Environment
主要分為增加配置或刪除配置:
def pytest_configure(config): # 添加配置 config._metadata["項(xiàng)目名稱"] = "測(cè)試報(bào)告" # 刪除配置 config._metadata.pop("JAVA_HOME") config._metadata.pop("Plugins") config._metadata.pop("Packages") config._metadata.pop("Platform")
修改 Results
從上面生成的報(bào)告列表中,看到主要分為下面幾列數(shù)據(jù):Result、Test、Links、Duration。這里面的數(shù)據(jù)其實(shí)可以看出都沒(méi)有包含我們的測(cè)試數(shù)據(jù),無(wú)法直觀看出輸入、輸出結(jié)果。
做如下優(yōu)化:
- 1.刪除 Test、Links 列
- 2.增加幾列分別展示參數(shù)化中的內(nèi)容,如用例編號(hào)、輸入、輸出
- 3.修改用例執(zhí)行結(jié)果 show details 中內(nèi)容,自定義展示內(nèi)容
基于上述需求,要在報(bào)告中添加我們自己的測(cè)試數(shù)據(jù)展示,故需要添加一個(gè)全局變量在一個(gè) case 執(zhí)行過(guò)程中進(jìn)行記錄供調(diào)用。
創(chuàng)建全局變量:
# 定義一個(gè)全局變量,用于存儲(chǔ)內(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ù),將測(cè)試數(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'))
生成效果報(bào)告
可以看到現(xiàn)在生成的報(bào)告內(nèi)容就可以清晰看到測(cè)試數(shù)據(jù),和我們的用例數(shù)據(jù)關(guān)聯(lián)上了。
后記
當(dāng)前只是簡(jiǎn)單的對(duì)報(bào)告展示的數(shù)據(jù)進(jìn)行了更改,感興趣可以查看官方文檔學(xué)習(xí)
https://docs.pytest.org/en/latest/reference/reference.html#hooks
以上就是pytest生成簡(jiǎn)單自定義測(cè)試結(jié)果html報(bào)告的詳細(xì)內(nèi)容,更多關(guān)于pytest生成自定義測(cè)試html的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中的os.mkdir和os.makedirs的使用區(qū)別及如何查看某個(gè)模塊中的某些字母開(kāi)頭的屬性方法
這篇文章主要介紹了python中的os.mkdir和os.makedirs的使用區(qū)別及如何查看某個(gè)模塊中的某些字母開(kāi)頭的屬性方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03關(guān)于python常見(jiàn)異常以及處理方法
這篇文章主要介紹了關(guān)于python常見(jiàn)異常以及處理方法,python用異常對(duì)象(exception object)來(lái)表示異常情況。遇到錯(cuò)誤后,會(huì)引發(fā)異常,需要的朋友可以參考下2023-04-04解決python3.x安裝numpy成功但import出錯(cuò)的問(wèn)題
這篇文章主要介紹了解決python3.x安裝numpy成功但import出錯(cuò)的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11用Python?Tkinter庫(kù)GUI編程創(chuàng)建圖形用戶界面
這篇文章主要為大家介紹了用Python?Tkinter庫(kù)GUI編程創(chuàng)建圖形用戶界面,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Python中pandas的dataframe過(guò)濾數(shù)據(jù)方法
這篇文章主要介紹了Python中pandas的dataframe過(guò)濾數(shù)據(jù)方法,Pandas是另外一個(gè)用于處理高級(jí)數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析的Python庫(kù),Pandas是基于Numpy構(gòu)建的一種工具,需要的朋友可以參考下2023-07-07Python中scatter散點(diǎn)圖及顏色整理大全
python自帶的scatter函數(shù)參數(shù)中顏色和大小可以輸入列表進(jìn)行控制,即可以讓不同的點(diǎn)有不同的顏色和大小,下面這篇文章主要給大家介紹了關(guān)于Python中scatter散點(diǎn)圖及顏色整理大全的相關(guān)資料,需要的朋友可以參考下2023-05-05