1. 程式人生 > >基於Python豆瓣自動化測試【2】

基於Python豆瓣自動化測試【2】

—— Pytest基礎使用教程【2】 ### 從測試報告說起 承接上一篇中最後的測試結果圖,使用過的pytest-html 外掛原生態的報告的話。可能會發現 內容樣式都有些不同。其實是做了些擴充套件相關設定所呈現的效果,當然可以定製的更深度一些,更加中文、本地化,又或者根據公司需要進行定向研發。例如就上文中的測試報告進行一些中文定製改造後效果如下圖所示。這篇就如何優化、定製pytest-html 測試報告進行些講解 ![](https://img2020.cnblogs.com/blog/409098/202005/409098-20200529191717278-1089182523.png) ### Pytest-html 擴充套件 目前最新的 pytest-html版本為2.1.1 。這個版本共提供 5個Hook,分別是: ```python def pytest_html_report_title(report) ``` 設定測試報告的標題 ```python def pytest_html_results_summary(prefix, summary, postfix) ``` 在Summary部分,新增自定義內容 ```python def pytest_html_results_table_header(cells) ``` 定製 Result 部分,表單的頭部 ```python def pytest_html_results_table_row(report, cells) ``` 定製Result部分,每行測試Case的內容 ```python def pytest_html_results_table_html(report, data) ``` 在完成Result渲染後,詳情新增寫HTMl標記語言內容 ### 測試報告Title 所謂Title指代的是報告圖中【豆瓣網自動化測試示例(基於Pytest)】行文字內容。Report 其實是 外掛的HTMLReport物件,簡單看下原始碼,相信使用上就能很好的把握理解。關鍵部分見紅框 ![](https://img2020.cnblogs.com/blog/409098/202005/409098-20200529191754423-1661422431.png) 所以,這個擴充套件而言只需要 conftest.py 對於擴充套件hook中設定下report title欄位就能夠,按需修改標題。 ```python @pytest.hookimpl(optionalhook=True) def pytest_html_report_title(report): report.title = "豆瓣網自動化測試示例(基於Pytest)" ``` ### 擴充套件Summary部分 Summary擴充套件設計,允許對於 整個模組進行定製。分為前(prefix)、中(summary)、後(postfix)三個部分進行設定。同樣的 看下原始碼,更好的把握如何來使用。關鍵部分見紅框 ![](https://img2020.cnblogs.com/blog/409098/202005/409098-20200529191806610-1474122657.png) 其實,就是把 前中後三個部分的html拼接起來,中部(summary) 會有些外掛預設的html內容。 ![](https://img2020.cnblogs.com/blog/409098/202005/409098-20200529191822232-2058132239.png) 所以,擴充套件使用上就很明晰了,只需要把html設定進去即可。 ```python @pytest.hookimpl(optionalhook=True) def pytest_html_results_summary(prefix, summary, postfix): prefix.extend([html.p("測試人: 姜子軒")]) ``` ### Result 表格的擴充套件 可以分成兩部分,一部分是表頭的設定。同樣的通過 cells 來生成 Result 表格的頭部。 ![](https://img2020.cnblogs.com/blog/409098/202005/409098-20200529192003126-1941584883.png) 看完上述原始碼相信,pytest_html_results_table_header 使用上就非常明確了。主要就是對cells進行操作。 ```python @pytest.hookimpl(optionalhook=True) def pytest_html_results_table_header(cells): cells.insert(2, html.th('模組')) cells.insert(3, html.th('描述')) cells.insert(4, html.th('時間', class_='sortable time', col='time')) cells.pop() ``` 對於每一行資料進行擴充套件上,也就是 pytest_html_results_table_row、pytest_html_results_table_html。這兩個的使用,同樣先看下原始碼。 ![](https://img2020.cnblogs.com/blog/409098/202005/409098-20200529192027241-1122576348.png) 其中兩個函式的關鍵點在於 report 引數,cells 與 data用來制定擴充套件的html標籤,而內容上通過 report 來進行透傳。所以 這裡一般會結合pytest內建的hook來使用,pytest_runtest_makereport。具體來說如下: {{uploading-image-119142.png(uploading...)}} 具體程式碼演示。 ```python @pytest.hookimpl(optionalhook=True) def pytest_html_results_table_row(report, cells): cells.insert(2, html.td(report.module)) cells.insert(3, html.td(report.description)) cells.insert(4, html.td(datetime.utcnow(), class_='col-time')) cells.pop() @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() report.description = str(item.function.__doc__) report.module = str(item.module.__doc__) ``` ### 深度定製 以上內容,可以看到Pytest-html v2.1.2版本 提供的全部擴充套件功能,能夠對 title、summary、table 進行內容擴充套件研發。不過,無法實現本文最前面全中文報告。所以 想要完美 的根據公司、業務需求改造,這裡提供一個思路方法可以將 Pytest-html 原始碼下載下來進行改造。其 整體的實現並不複雜,主要邏輯 在 plugin.py 裡面。 {{uploading-image-431444.png(uploading...)}} 其中,整個報告的生成 在 ***_generate_report 函式*** 中。在本篇中就不深入來說實現,計劃在後續 pytest外掛研發、Pytest-html實現中進一步分享 如果對本文 深度定製中文版報告 改造或者 是上文中擴充套件原始碼有興趣的可以關注 公眾號私信