golang中的TestMain
生活随笔
收集整理的這篇文章主要介紹了
golang中的TestMain
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
go test 功能,提高了開發和測試的效率。
有時會遇到這樣的場景:
進行測試之前需要初始化操作(例如打開連接),測試結束后,需要做清理工作(例如關閉連接)等等。這個時候就可以使用TestMain()。
下面例子的文件結構如下:
hello/add.go
hello/test_add.go
add.go
package hellofunc Add(a,b int) int {return a+b }add_test.go
package helloimport("fmt""testing" )func TestAdd(t *testing.T) {r := Add(1, 2)if r !=3 {t.Errorf("Add(1, 2) failed. Got %d, expected 3.", r)} }func TestMain(m *testing.M) {fmt.Println("begin")m.Run()fmt.Println("end") }復制
測試從TestMain進入
總結
以上是生活随笔為你收集整理的golang中的TestMain的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: golang中的反射
- 下一篇: golang中的测试命令