網站首頁 編程語言 正文
一、安裝插件
要生成html類型的報告,需要使用pytest-html插件,可以在IDE中安裝,也可以在命令行中安裝。插件安裝
的位置涉及到不同項目的使用,這里不再詳述,想了解的可自行查詢。
IDE中安裝
在File>Settings>Project>Project Interpreter界面,點擊“ + ”搜索pytest-html即可進行安裝。
命令行安裝
建議先在命令行中切換到python安裝路徑“ Lib\site-packages ”目錄,再執行安裝命令。
pip install -U pytest-html
二、生成html報告
先準備一個簡單的執行腳本
import pytest def fun(x): return x + 1 def test_answer_1(): """測試斷言一""" assert fun(3) == 4 def test_answer_2(): """測試斷言二""" assert fun(5) == 7 @pytest.mark.parametrize("test_input,expected",[ ("3+5",8), ("2+4",6), pytest.param("6 * 9",42,marks=pytest.mark.xfail), pytest.param("6 * 6",42,marks=pytest.mark.skip) ]) def test_mark(test_input,expected): """用例集合""" assert eval(test_input) == expected if __name__ == '__main__': pytest.main(['-v','--html=report.html','test_08.py'])
生成報告命令pytest --html=報告名稱 要執行的腳本文件 ,執行上述腳本查看結果。
report.html:報告名稱,記錄報告生成時間以及插件版本
Environment:測試環境
Summary:用例統計
Results:測試結果,點擊Show all details / Hide all details可以展開結果詳情或收縮全部結果
三、使用小技巧
指定路徑
通過上述命令運行腳本后可以發現,測試報告保存在項目的根目錄下,查找報告比較繁瑣。我們可以
在運行命令中指定報告路徑pytest -v --html=./outputs/report.html test_08.py,代碼執行完成,
可以發現項目根目錄下生成了outputs文件,測試報告也在其中。
報告獨立
當本地執行完成,想把測試報告分享出去,卻發現分享出去的報告打開后樣式丟失。因為代碼執行完成
會生成assets文件,將CSS保存在了本地。我們可以通過命令將CSS寫入HTML中,這樣生成的測試報告就能
對外分享了。
pytest -v --html=./outputs/report.html --self-contained-html test_08.py
四、報告優化
在實際的工作中,通過上述操作生成的測試報告一般不是我們想要的結果。環境信息通過增減更換成需
要展示的內容、增加用例描述、去掉多余的列等等。這里需要將優化代碼寫入conftest.py文件,該文件名是固
定的不可更改。
導入引用包
import pytest from py._xmlgen import html from datetime import datetime
修改測試環境
@pytest.mark.parametrize def pytest_configure(config): config._metadata.pop("JAVA_HOME") # 刪除java_home config._metadata["項目名稱"] = "引擎自動化" # 添加項目名稱 config._metadata["接口地址"] = "https://www.example.com/poke" # 添加接口地址
修改用例統計
@pytest.mark.parametrize def pytest_html_results_summary(prefix,summary,postfix): prefix.extend([html.p("所屬部門:測試組")]) prefix.extend([html.p("測試人員:許衛玲")])
修改結果顯示
@pytest.mark.optionalhook def pytest_html_results_table_header(cells): cells.insert(1,html.th("Description")) # 表頭添加Description cells.insert(2,html.th("Time",class_="sortable time",col="time")) cells.pop(-1) # 刪除link @pytest.mark.optionalhook def pytest_html_results_table_row(report,cells): cells.insert(1,html.td(report.description)) # 表頭對應的內容 cells.insert(2,html.td(datetime.now(),class_="col-time")) cells.pop(-1) # 刪除link @pytest.mark.hookwrapper def pytest_runtest_makereport(item,call): # Description取值為用例說明__doc__ outcome = yield report = outcome.get_result() report.description = str(item.function.__doc__) report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
修改完成,重新執行腳本,查看最終效果。
作者:Sweettesting —— 半醉半醒半浮生
出處:http://www.cnblogs.com/Sweettesting/
原文鏈接:https://www.cnblogs.com/Sweettesting/p/15843803.html
相關推薦
- 2023-07-30 el-table自定義合并行或列
- 2022-09-01 PgSQL條件語句與循環語句示例代碼詳解_PostgreSQL
- 2022-05-11 使用Redission實現分布式鎖
- 2022-10-01 React?hooks?useState異步問題及解決_React
- 2022-06-09 Nginx動靜分離配置實現與說明_nginx
- 2022-11-16 anaconda打開閃退的解決過程_python
- 2022-06-06 ASP.NET的Core?AD域登錄過程示例_ASP.NET
- 2022-09-25 linux系統下oracle數據庫的導入導出
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支