企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 使用第三方测试工具 Go测试有许多有用的工具。 这些工具可以让你更容易地了解每个功能级别的代码覆盖率,还可以使用断言工具来减少测试代码行。 本文将介绍 github.com/axw/gocov 和github.com/smartystreets/goconvey 软件包,以展示其中的一些功能。此外 github.com/smartystreets/goconvey 包支持断言和运行时测试。 ### 实践 1.获取第三方库: ``` go get github.com/axw/gocov go get github.com/smartystreets/goconvey/ ``` 2. 建立 funcs.go: ``` package tools import ( "fmt" ) func example() error { fmt.Println("in example") return nil } var example2 = func() int { fmt.Println("in example2") return 10 } ``` 3. 建立 structs.go: ``` package tools import ( "errors" "fmt" ) type c struct { Branch bool } func (c *c) example3() error { fmt.Println("in example3") if c.Branch { fmt.Println("branching code!") return errors.New("bad branch") } return nil } ``` 4. 建立 funcs_test.go: ``` package tools import ( "testing" . "github.com/smartystreets/goconvey/convey" ) func Test_example(t *testing.T) { tests := []struct { name string }{ {"base-case"}, } for _, tt := range tests { Convey(tt.name, t, func() { res := example() So(res, ShouldBeNil) }) } } func Test_example2(t *testing.T) { tests := []struct { name string }{ {"base-case"}, } for _, tt := range tests { Convey(tt.name, t, func() { res := example2() So(res, ShouldBeGreaterThanOrEqualTo, 1) }) } } ``` 5. 建立 structs_test.go: ``` package tools import ( "testing" . "github.com/smartystreets/goconvey/convey" ) func Test_c_example3(t *testing.T) { type fields struct { Branch bool } tests := []struct { name string fields fields wantErr bool }{ {"no branch", fields{false}, false}, {"branch", fields{true}, true}, } for _, tt := range tests { Convey(tt.name, t, func() { c := &c{ Branch: tt.fields.Branch, } So((c.example3() != nil), ShouldEqual, tt.wantErr) }) } } ``` 6. 运行: ``` $ gocov test | gocov report ok github.com/agtorre/go-cookbook/chapter8/tools 0.006s coverage: 100.0% of statements github.com/agtorre/go-cookbook/chapter8/tools/struct.go c.example3 100.00% (5/5) github.com/agtorre/go-cookbook/chapter8/tools/funcs.go example 100.00% (2/2) github.com/agtorre/go-cookbook/chapter8/tools/funcs.go @12:16 100.00% (2/2) github.com/agtorre/go-cookbook/chapter8/tools ---------- 100.00% (9/9) Total Coverage: 100.00% (9/9) ``` 7. 执行goconvey命令,会在浏览器中显示: ![](https://box.kancloud.cn/21b9c114362d1c98846b13801bd9e9e7_712x367.jpg) ### 说明 本节演示了如何使用goconvry。与前面的小节相比,Convey关键字基本替代了t.Run,并且可以生成在goconvey生成的UI中显示的标签,但与t,Run的表现有所不同。如果你有嵌套的这样测试块: ``` Convey("Outer loop", t, func(){ a := 1 Convey("Inner loop", t, func() { a = 2 }) Convey ("Inner loop2", t, func(){ fmt.Println(a) }) }) ``` 使用goconvey命令,将打印1。如果我们使用t.Run,将打印2。换句话说,t.Run按顺序运行测试,永远不会重复。此行为对于将变量设置在外部代码块中非常有用。如果混合使用,就必须记住这种区别。 在使用convey断言时,在UI中有成功的复选标记和其他统计信息。使用它还可以减少if检查单行的大小,甚至可以创建自定义断言。 如果保留goconvey Web界面并打开通知,则在保存代码时,将自动运行测试,并且将收到有关任何增加或减少覆盖范围以及构建失败时的通知。 以上功能都可以单独或一起使用。 在努力提高测试覆盖率时,gocov工具非常有用。它可以快速识别尚未测试的函数,并帮助了解覆盖率报告。此外,gocov可用于生成使用 github.com/matm/gocov-html 包随Go代码一起提供的备用HTML报告。 * * * * 学识浅薄,错误在所难免。欢迎在群中就本书提出修改意见,以飨后来者,长风拜谢。 Golang中国(211938256) beego实战(258969317) Go实践(386056972)