basics

  • 测试的代码文件和源代码文件在同一目录下, 名称为xxx_test.go,比如测试fib.go文件,则测试文件为fib_test.go

  • 测试函数名称为func TestXXX(t *testing.T)

  • 使用测试用例表是测试代码更整洁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func TestYourFunc(t *testing.T) {
cases := []struct{
intput int
result int
}{
{
0,
0,
}
{
1,
2,
},
...
}

for _, c := range cases {
result := YourFunc(c.input)
if result != c.result {
t.Errorf("Expect: %d, get: %d\n", c.result, result)
}
}
}

性能测试

  • 在测试代码中以func BenchmarkYourFunc(b *testing.B)开头

example

现在有一个测试计算第n个fib数的函数

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
package gotest

import (
"fmt"
)

// MyFib 计算第n个斐波那契数
// https://www.wikiwand.com/zh/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97
func MyFib(n int) (int32, error) {
if n < 0 {
return -1, fmt.Errorf("n is negtive")
}

fibNumbers := []int32{0, 1, 1}

if n <= 2 {
return fibNumbers[n], nil
}

return fib(n, fibNumbers), nil
}

func fib(n int, nums []int32) int32 {
switch n {
case 0, 1, 2:
return nums[n]
default:
return fib(n-1, nums) + fib(n-2, nums)
}
}

fib_test.go

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package gotest

import (
"testing"
)

func TestMyFib(t *testing.T) {
cases := []struct {
n int
result int32
}{
{
0,
0,
},
{
1,
1,
},
{
2,
1,
},
{
3,
2,
},
{
4,
3,
},
{
5,
5,
},
{
10,
55,
},
{
20,
6765,
},
}

_, err := MyFib(-1)
if err == nil {
t.Errorf("error should not be nil\n")
}

for _, c := range cases {
result, err := MyFib(c.n)
if err != nil {
t.Errorf("MyFib error: %v\n", err)
}

t.Logf("result: %d\n", result)

if result != c.result {
t.Errorf("expected %d, get: %d\n", c.result, result)
}
}
}

func BenchmarkMyFib5(b *testing.B) {
for n := 0; n < b.N; n++ {
MyFib(5)
}
}

func BenchmarkMyFib10(b *testing.B) {
for n := 0; n < b.N; n++ {
MyFib(10)
}
}

func BenchmarkMyFib20(b *testing.B) {
for n := 0; n < b.N; n++ {
MyFib(20)
}
}

func BenchmarkMyFib30(b *testing.B) {
for n := 0; n < b.N; n++ {
MyFib(30)
}
}

go test 测试结果

1
2
3
go test
PASS
ok golangtest/gotest 0.007s

go test -v

1
2
3
4
5
6
7
8
9
10
11
12
13
go test -v
=== RUN TestMyFib
--- PASS: TestMyFib (0.00s)
fib_test.go:57: result: 0
fib_test.go:57: result: 1
fib_test.go:57: result: 1
fib_test.go:57: result: 2
fib_test.go:57: result: 3
fib_test.go:57: result: 5
fib_test.go:57: result: 55
fib_test.go:57: result: 6765
PASS
ok golangtest/gotest 0.007s

测试覆盖率

1
2
3
4
go test -cover
PASS
coverage: 100.0% of statements
ok golangtest/gotest 0.007s

性能测试

1
2
3
4
5
6
7
8
9
10
go test -bench=.
goos: darwin
goarch: amd64
pkg: golangtest/gotest
BenchmarkMyFib5-8 50000000 36.0 ns/op
BenchmarkMyFib10-8 3000000 434 ns/op
BenchmarkMyFib20-8 30000 53466 ns/op
BenchmarkMyFib30-8 200 6580229 ns/op
PASS
ok golangtest/gotest 7.697s

指定测试匹配名称的函数

go test -run=FuncName

1
2
3
4
5
6
7
go test -bench=BenchmarkMyFib20
goos: darwin
goarch: amd64
pkg: golangtest/gotest
BenchmarkMyFib20-8 30000 53025 ns/op
PASS
ok golangtest/gotest 2.110s