網(wǎng)站首頁 編程語言 正文
基本概念
上一節(jié)提到,代碼完成的標(biāo)準(zhǔn)之一還包含了單元測試,這部分也是很多開發(fā)流程中不規(guī)范的地方。寫過單元測試的開發(fā)人員應(yīng)該理解,單元測試最核心的價值是為了證明:為什么我寫的代碼是正確的?也就是從邏輯角度幫你檢查你的代碼。但是另外一方面,如果從單元測試覆蓋率角度來看,單元測試也是非常耗時的,幾乎是三倍于你代碼的開發(fā)時間,所以在很多迭代速度非常快的項(xiàng)目中,單元測試就幾乎沒人要求了。但是單元測試真的能夠從根本上提高代碼質(zhì)量,降低低級錯誤出現(xiàn)的概率。
示例一:取整函數(shù)基本測試
前置條件
Go語言內(nèi)置了單元測試執(zhí)行的指令,由于尚未使用Go Modules方法,我們?nèi)匀灰O(shè)置環(huán)境變量,才能正確進(jìn)行測試
export GO111MODULE=off go test
代碼
假設(shè)我們對以下函數(shù)進(jìn)行測試
package even func Even(i int) bool { return i % 2 == 0 }
單元測試建立步驟
創(chuàng)建一個單元測試,包括如下步驟:
- 在相同目錄下創(chuàng)建一個名為*_test.go的文件
- 執(zhí)行g(shù)o test進(jìn)行測試,將自動識別這些文件
- 引入testing包
- 每一個Case的命名都是以func TestXxx(t *testing.T)
編寫單元測試
這里分別對兩種場景進(jìn)行測試,一種是為偶數(shù)的情況,一種是為奇數(shù)的情況,來檢查我們的程序是否按照預(yù)期返回,如果不是則拋出異常信息
package even import "testing" func TestEven(t *testing.T) { if !Even(2) { t.Log("2 should be even!") t.Fail() } } func TestNotEven(t *testing.T) { if Even(3) { t.Log("3 should not be even!") t.Fail() } }
執(zhí)行g(shù)o test后
PASS
ok ??? ?_/root/workspace/go/test_unittest?? ?0.003s
示例二:Fail()函數(shù)
func (t *T) Fail() 讓測試失敗,同一個測試用例中的測試?yán)^續(xù)執(zhí)行,后續(xù)的測試也會繼續(xù)執(zhí)行
package even import "testing" func TestTestingFail(t *testing.T) { // Let create a fake case, we will call FailNow if Even(2) { t.Log("All test cases after Fail will still run") t.Fail() } if Even(2) { t.Log("The test after Fail will still run") t.Fail() } } func TestAfterFailCase(t *testing.T) { if Even(2) { t.Log("This test case after Fail will still run") t.Fail() } }
執(zhí)行測試后,TestTestingFail中的第二部分也可以繼續(xù)執(zhí)行。
--- FAIL: TestTestingFail (0.00s)
? ? even_fail_test.go:8: All test cases after Fail will still run
? ? even_fail_test.go:13: The test after Fail will still run
--- FAIL: TestAfterFailCase (0.00s)
? ? even_fail_test.go:20: This test case after Fail will still run
FAIL
exit status 1
FAIL?? ?_/root/workspace/go/test_unittest?? ?0.004s
示例三:FailNow函數(shù)
func (t *T) FailNow() 讓測試失敗,同一個測試用例中的測試不再執(zhí)行,后續(xù)的測試也會繼續(xù)執(zhí)行
package even import "testing" func TestTestingFailNow(t *testing.T) { // Let create a fake case, we will call FailNow if Even(2) { t.Log("All test cases after FailNow will not run") t.FailNow() } if Even(2) { t.Log("The test after FailNow will be skipped") t.FailNow() } } func TestAfterFailNowCase(t *testing.T) { if Even(2) { t.Log("This test case after FailNow will still run") t.FailNow() } }
執(zhí)行后TestTestingFailNow中的第二段測試不再執(zhí)行,而后面的TestAfterFailNowCase繼續(xù)執(zhí)行
--- FAIL: TestTestingFailNow (0.00s)
? ? even_failnow_test.go:8: All test cases after FailNow will not run
--- FAIL: TestAfterFailNowCase (0.00s)
? ? even_failnow_test.go:20: This test case after FailNow will still run
FAIL
exit status 1
FAIL?? ?_/root/workspace/go/test_unittest?? ?0.003s
實(shí)例四:Log和Fetal函數(shù)
func (t *T) Log(args …interface{}) 使用默認(rèn)格式記錄日志,等同于Print(),記錄錯誤日志
func (t *T) Fatal(args …interface{}) 與Log功能相似,但是輸出日志后會調(diào)用FailNow
package even import "testing" func TestTestingFatal(t *testing.T) { // Let create a fake case, we will call FailNow if Even(2) { t.Fatal("All test cases after FailNow will not run") } if Even(2) { t.Fatal("The test after Fatal will not run") } } func TestAfterFatalCase(t *testing.T) { if Even(2) { t.Fatal("This test case after Fatal will still run") } }
Fatal的執(zhí)行過程與FailNow相似
--- FAIL: TestTestingFatal (0.00s)
? ? even_fatal_test.go:8: All test cases after FailNow will not run
--- FAIL: TestAfterFatalCase (0.00s)
? ? even_fatal_test.go:18: This test case after Fatal will still run
FAIL
exit status 1
FAIL?? ?_/root/workspace/go/test_unittest?? ?0.005s
原文鏈接:https://blog.csdn.net/xiaoquqi/article/details/125816871
相關(guān)推薦
- 2022-11-15 Rust使用kind進(jìn)行異常處理(錯誤的分類與傳遞)_相關(guān)技巧
- 2022-06-12 Dockerfile文件編寫及構(gòu)建鏡像命令解析_docker
- 2022-06-16 HTTP服務(wù)壓力測試工具及相關(guān)術(shù)語講解_Golang
- 2023-01-15 Keras中Conv1D的使用及說明_python
- 2022-09-14 Python字符串拼接的4種方法實(shí)例_python
- 2022-11-21 詳解如何使用Python實(shí)現(xiàn)刪除重復(fù)文件_python
- 2022-11-16 python中內(nèi)置類型添加屬性問題詳解_python
- 2022-03-25 Unity實(shí)現(xiàn)圓形Image組件_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支