# golang 单元测试
TIP
测试很重要!!!
# 待测的文件 main.go
package testdemo
import "fmt"
func IntMin(a, b int) int {
if a < b {
return a
} else {
return b
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 测试文件编写 main_test.go
package testdemo
import (
"fmt"
"testing"
)
// 通常测试函数的名字以Test开头
func TestIntMinBasic(t *testing.T) {
ans := IntMin(2, -2)
if ans != -2 {
t.Errorf("IntMin(2, -2) = %d; want -2", ans)
}
}
// 使用 `t.Run()` 来创建子测试
func TestIntMinTableDriven(t *testing.T) {
var tests := []struct {
a, b, want int
}{
{0,1,0},
{0,-1,-1},
{110,10,10},
{101,31,31},
}
for _, tt := range tests {
testname = fmt.Sprintf("%d, %d", tt.a, tt.b)
t.Run(testname, func(t *testing.T) {
ans := IntMin(tt.a, tt.b)
if ans != tt.want {
t.Errof("got %d , want %d", ans, tt.want)
}
})
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
TIP
- 上面的测试仅仅是官方库的版本
- 一般生产环境中使用测试框架