網(wǎng)站首頁 編程語言 正文
一、安裝插件
要生成html類型的報告,需要使用pytest-html插件,可以在IDE中安裝,也可以在命令行中安裝。插件安裝
的位置涉及到不同項目的使用,這里不再詳述,想了解的可自行查詢。
IDE中安裝
在File>Settings>Project>Project Interpreter界面,點擊“ + ”搜索pytest-html即可進行安裝。
命令行安裝
建議先在命令行中切換到python安裝路徑“ Lib\site-packages ”目錄,再執(zhí)行安裝命令。
pip install -U pytest-html
二、生成html報告
先準備一個簡單的執(zhí)行腳本
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=報告名稱 要執(zhí)行的腳本文件 ,執(zhí)行上述腳本查看結(jié)果。
report.html:報告名稱,記錄報告生成時間以及插件版本
Environment:測試環(huán)境
Summary:用例統(tǒng)計
Results:測試結(jié)果,點擊Show all details / Hide all details可以展開結(jié)果詳情或收縮全部結(jié)果
三、使用小技巧
指定路徑
通過上述命令運行腳本后可以發(fā)現(xiàn),測試報告保存在項目的根目錄下,查找報告比較繁瑣。我們可以
在運行命令中指定報告路徑pytest -v --html=./outputs/report.html test_08.py,代碼執(zhí)行完成,
可以發(fā)現(xiàn)項目根目錄下生成了outputs文件,測試報告也在其中。
報告獨立
當本地執(zhí)行完成,想把測試報告分享出去,卻發(fā)現(xiàn)分享出去的報告打開后樣式丟失。因為代碼執(zhí)行完成
會生成assets文件,將CSS保存在了本地。我們可以通過命令將CSS寫入HTML中,這樣生成的測試報告就能
對外分享了。
pytest -v --html=./outputs/report.html --self-contained-html test_08.py
四、報告優(yōu)化
在實際的工作中,通過上述操作生成的測試報告一般不是我們想要的結(jié)果。環(huán)境信息通過增減更換成需
要展示的內(nèi)容、增加用例描述、去掉多余的列等等。這里需要將優(yōu)化代碼寫入conftest.py文件,該文件名是固
定的不可更改。
導入引用包
import pytest from py._xmlgen import html from datetime import datetime
修改測試環(huán)境
@pytest.mark.parametrize def pytest_configure(config): config._metadata.pop("JAVA_HOME") # 刪除java_home config._metadata["項目名稱"] = "引擎自動化" # 添加項目名稱 config._metadata["接口地址"] = "https://www.example.com/poke" # 添加接口地址
修改用例統(tǒng)計
@pytest.mark.parametrize def pytest_html_results_summary(prefix,summary,postfix): prefix.extend([html.p("所屬部門:測試組")]) prefix.extend([html.p("測試人員:許衛(wèi)玲")])
修改結(jié)果顯示
@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)) # 表頭對應的內(nèi)容 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")
修改完成,重新執(zhí)行腳本,查看最終效果。
作者:Sweettesting —— 半醉半醒半浮生
出處:http://www.cnblogs.com/Sweettesting/
原文鏈接:https://www.cnblogs.com/Sweettesting/p/15843803.html
相關推薦
- 2022-05-20 SpringCloud系列:springboot改造集成nacos
- 2022-12-04 在python?list中篩選包含字符的字段方式_python
- 2022-04-23 window.open打開新窗口設置顯示位置及大小
- 2023-04-18 antd中form表單的wrapperCol和labelCol問題詳解_React
- 2022-05-02 C++?多繼承詳情介紹_C 語言
- 2022-10-20 Flutter實現(xiàn)矩形取色器的封裝_Android
- 2024-04-07 linux系統(tǒng)中防火墻(firewall)的操作(開啟端口,查看端口)
- 2023-03-02 Python實現(xiàn)設置顯示屏分辨率_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支