## 使用第三方測試工具
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命令,會在瀏覽器中顯示:

### 說明
本節演示了如何使用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)
- 前言
- 第一章 I/O和文件系統
- 常見 I/O 接口
- 使用bytes和strings包
- 操作文件夾和文件
- 使用CSV格式化數據
- 操作臨時文件
- 使用 text/template和HTML/templates包
- 第二章 命令行工具
- 解析命令行flag標識
- 解析命令行參數
- 讀取和設置環境變量
- 操作TOML,YAML和JSON配置文件
- 操做Unix系統下的pipe管道
- 處理信號量
- ANSI命令行著色
- 第三章 數據類型轉換和解析
- 數據類型和接口轉換
- 使用math包和math/big包處理數字類型
- 貨幣轉換和float64注意事項
- 使用指針和SQL Null類型進行編碼和解碼
- 對Go數據編碼和解碼
- Go中的結構體標簽和反射
- 通過閉包實現集合操作
- 第四章 錯誤處理
- 錯誤接口
- 使用第三方errors包
- 使用log包記錄錯誤
- 結構化日志記錄
- 使用context包進行日志記錄
- 使用包級全局變量
- 處理恐慌
- 第五章 數據存儲
- 使用database/sql包操作MySQL
- 執行數據庫事務接口
- SQL的連接池速率限制和超時
- 操作Redis
- 操作MongoDB
- 創建存儲接口以實現數據可移植性
- 第六章 Web客戶端和APIs
- 使用http.Client
- 調用REST API
- 并發操作客戶端請求
- 使用OAuth2
- 實現OAuth2令牌存儲接口
- 封裝http請求客戶端
- 理解GRPC的使用
- 第七章 網絡服務
- 處理Web請求
- 使用閉包進行狀態處理
- 請求參數驗證
- 內容渲染
- 使用中間件
- 構建反向代理
- 將GRPC導出為JSON API
- 第八章 測試
- 使用標準庫進行模擬
- 使用Mockgen包
- 使用表驅動測試
- 使用第三方測試工具
- 模糊測試
- 行為驅動測試
- 第九章 并發和并行
- 第十章 分布式系統
- 第十一章 響應式編程和數據流
- 第十二章 無服務器編程
- 第十三章 性能改進