Golang測(cè)試框架goconvey進(jìn)行單元測(cè)試流程介紹
導(dǎo)包
import “github.com/smartystreets/goconvey”
核心API
頂層Convey:由測(cè)試描述、testing.T,行為函數(shù)組成。
Convey(description string, t *testing.T, action func())
其他層Convey:
Convey(description string, action func())
值斷言:判斷actual值是否符合預(yù)期。
func So(actual any, assert Assertion, expected ...any)
actual:實(shí)際值。
Assertion:斷言條件,一般為ShouldXXX組成,
expect:預(yù)期值。
convey運(yùn)行順序
由樹(shù)形結(jié)構(gòu)進(jìn)行遍歷
Convey A
So 1
Convey B
So 2
Convey C
So 3
執(zhí)行順序:1 A->B 、2 A->C
代碼示例
1.測(cè)試x++
func TestGetSumScore(t *testing.T) { Convey("start x is 0", t, func() { x := 0 Convey("x++", func() { x++ So(x, ShouldEqual, 1) }) }) }
2.使用多層嵌套:測(cè)試GetSumScore函數(shù)
GetSumScore函數(shù)實(shí)現(xiàn):
type Student struct { ID int64 Name string Age int8 Major string Score int } // 返回這些學(xué)生的分?jǐn)?shù)總和 func GetSumScore(students []Student) int { total := 0 for _, v := range students { total += v.Score } return total }
測(cè)試代碼:
func TestGetSumScore(t *testing.T) { convey.Convey("init students", t, func() { students := []Student{ {Name: "yi", Score: 90}, {Name: "w", Score: 100}, } score := GetSumScore(students) convey.Convey("GetSumScore", func() { convey.So(score, convey.ShouldEqual, 190) }) convey.Convey("Change students[0].score", func() { students[0].Score = 10 score := GetSumScore(students) convey.So(score, convey.ShouldEqual, 110) }) }) }
到此這篇關(guān)于Golang測(cè)試框架goconvey進(jìn)行單元測(cè)試流程介紹的文章就介紹到這了,更多相關(guān)Golang goconvey內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang使用gin框架實(shí)現(xiàn)一個(gè)完整的聊天室功能
由于我們項(xiàng)目的需要,我就研究了一下關(guān)于websocket的相關(guān)內(nèi)容,去實(shí)現(xiàn)一個(gè)聊天室的功能,經(jīng)過(guò)幾天的探索,現(xiàn)在使用Gin框架實(shí)現(xiàn)了一個(gè)完整的聊天室+消息實(shí)時(shí)通知系統(tǒng),感興趣的小伙伴歡迎閱讀本文2023-08-08Go外部依賴包從vendor,$GOPATH和$GOPATH/pkg/mod查找順序
這篇文章主要介紹了Go外部依賴包vendor,$GOPATH和$GOPATH/pkg/mod下查找順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12大多數(shù)Go程序員都走過(guò)的坑盤(pán)點(diǎn)解析
這篇文章主要為大家介紹了大多數(shù)Go程序員都走過(guò)的坑盤(pán)點(diǎn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Go-家庭收支記賬軟件項(xiàng)目實(shí)現(xiàn)
這篇文章主要介紹了Go-家庭收支記賬軟件項(xiàng)目實(shí)現(xiàn),本文章內(nèi)容詳細(xì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,需要的朋友可以參考下2023-01-01GO語(yǔ)言實(shí)現(xiàn)二維碼掃碼的示例代碼
你對(duì)二維碼掃碼的流程有困惑嗎,這篇文章就結(jié)合筆者自身的開(kāi)發(fā)經(jīng)驗(yàn)進(jìn)行分享,讓大家熟悉并掌握此功能,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2023-06-06