package
Version:
v0.0.0-...-95a312b
Opens a new window with list of versions in this module.
Published: Mar 16, 2023
License: MIT
Opens a new window with license information.
Imports: 3
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
golang单元测试整理
golang基本的单元测试
- go语言的单元测试采用内置的测试框架,通过引入testing包以及go test来提供测试功能。
- _test.go为后缀名的源文件被go test认定为测试文件,这些文件不包含在go build的代码构建中,而是单独通过 go test来编译,执行。
使用gotest生成表格驱动的测试用例
-
gotests详细介绍
-
gotests使用
-
创建slice.go
-
使用gotests根据slice.go的内容创建slice_test.go
gotests -all slice.go -w slice_test.go
生成代码类似如下内容:
func TestAdd(t *testing.T) {
type args struct {
s []string
a string
}
tests := []struct {
name string
args args
want []string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Add(tt.args.s, tt.args.a); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Add() = %v, want %v", got, tt.want)
}
})
}
}
mock的使用实践
HTTP服务单元测试
代码覆盖率
参考资料
Documentation
¶
如果string类型的a在切片中不存在,那么就添加到切片
type Person struct {
Name string `json:"name"`
Address string `json:"address"`
Age int `json:"age"`
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.