一文帶你了解Go語言中的單元測試
基本概念
上一節(jié)提到,代碼完成的標準之一還包含了單元測試,這部分也是很多開發(fā)流程中不規(guī)范的地方。寫過單元測試的開發(fā)人員應該理解,單元測試最核心的價值是為了證明:為什么我寫的代碼是正確的?也就是從邏輯角度幫你檢查你的代碼。但是另外一方面,如果從單元測試覆蓋率角度來看,單元測試也是非常耗時的,幾乎是三倍于你代碼的開發(fā)時間,所以在很多迭代速度非??斓捻椖恐?,單元測試就幾乎沒人要求了。但是單元測試真的能夠從根本上提高代碼質(zhì)量,降低低級錯誤出現(xiàn)的概率。
示例一:取整函數(shù)基本測試
前置條件
Go語言內(nèi)置了單元測試執(zhí)行的指令,由于尚未使用Go Modules方法,我們?nèi)匀灰O置環(huán)境變量,才能正確進行測試
export GO111MODULE=off go test
代碼
假設我們對以下函數(shù)進行測試
package even func Even(i int) bool { return i % 2 == 0 }
單元測試建立步驟
創(chuàng)建一個單元測試,包括如下步驟:
- 在相同目錄下創(chuàng)建一個名為*_test.go的文件
- 執(zhí)行go test進行測試,將自動識別這些文件
- 引入testing包
- 每一個Case的命名都是以func TestXxx(t *testing.T)
編寫單元測試
這里分別對兩種場景進行測試,一種是為偶數(shù)的情況,一種是為奇數(shù)的情況,來檢查我們的程序是否按照預期返回,如果不是則拋出異常信息
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í)行go test后
PASS
ok _/root/workspace/go/test_unittest 0.003s
示例二:Fail()函數(shù)
func (t *T) Fail() 讓測試失敗,同一個測試用例中的測試繼續(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
實例四:Log和Fetal函數(shù)
func (t *T) Log(args …interface{}) 使用默認格式記錄日志,等同于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
到此這篇關于一文帶你了解Go語言中的單元測試的文章就介紹到這了,更多相關Go單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
優(yōu)雅使用GoFrame共享變量Context示例詳解
這篇文章主要為大家介紹了優(yōu)雅使用GoFrame共享變量Context示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Golang 經(jīng)典校驗庫 validator 用法解析
這篇文章主要為大家介紹了Golang 經(jīng)典校驗庫 validator 用法解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08Golang高性能持久化解決方案BoltDB數(shù)據(jù)庫介紹
這篇文章主要為大家介紹了Golang高性能持久化解決方案BoltDB數(shù)據(jù)庫介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11