go mod与单元测试

使用go mod 解决go模块间的引用,和go的单元测试

go mod 与单元测试

go mod

为解决go模块间的相互引用,可以将go mod理解成j2ee中的pom文件。

创建mod

默认模块名

1
go mod init

指定模块名

1
go mod init <model-name>

引入其他模块

比如说需要引入 gin 模块。

  1. 首先需要进入模块所在目录,运行命令:
1
go get -u github.com/gin-gonic/gin
  1. 查看mod文件

可以看到会将相关的依赖拉取下来,这样就能在自己的模块中使用这些包了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module org.golearn.basic

go 1.14

require (
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)

go 单元测试

创建源文件和测试文件

1
2
3
├── test
│ ├── calc.go
│ └── calc_test.go

calc.go

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

func Add(a, b int) int{
return a + b
}

func Sub(a, b int) int {
return a - b
}

func Mul(a, b int) int {
return a * b
}

calc_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
package main

import "testing"

func TestAdd(t *testing.T) {
t.Helper()
if ans := Add(1, 2); ans != 3 {
t.Errorf("1 + 2 expected be 3, but %d got", ans)
}
}

func TestMul(t *testing.T) {
//if ans := Mul(10, 20); ans != 100 {
// t.Errorf("10 * 20 expected be 600, but %d got", ans)
//}

// 子测试
// 正数
t.Run("正数相乘结果", func(t *testing.T) {
if ans := Mul(2, 3); ans != 6{
t.Errorf("10 * 20 expected be 6, but %d got", ans)
}
})

// 子测试-负数
t.Run("负数相乘结果", func(t *testing.T) {
if ans := Mul(-20, 3); ans != -60 {
t.Errorf("10 * 20 expected be -60, but %d got", ans)
}
})

}

func TestSub(t *testing.T) {
if ans := Sub(10, 1); ans != 9 {
t.Errorf("10 - 1 expected be 9, but %d got ", ans)

}
}

// 多个子测试场景
func TestMul2(t *testing.T) {

mulCases := []struct{
Name string
Num1, Num2, Expect int
}{
{"pos", 1, 2, 2},
{"neg", 1, -2, -2},
{"zero", 0, -2, 10},

}

for _, r := range mulCases {
t.Run(r.Name, func(t *testing.T) {
t.Helper()
if ans := Mul(r.Num1, r.Num2); ans != r.Expect {
t.Fatalf("%d * %d expected %d, but %d got",
r.Num1, r.Num2, r.Expect, ans)
}
})
}

}

//
/*
测试用例
1. 在相同的包路径中创建 xxx_test.go文件
2. 编写测试用例
3. 运行测试用例,查看测试用例中所有函数的运行结果 go test -v
*/

运行测试用例

1
go test -v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
=== RUN TestMul
=== RUN TestMul/正数相乘结果
=== RUN TestMul/负数相乘结果
--- PASS: TestMul (0.00s)
--- PASS: TestMul/正数相乘结果 (0.00s)
--- PASS: TestMul/负数相乘结果 (0.00s)
=== RUN TestSub
--- PASS: TestSub (0.00s)
=== RUN TestMul2
=== RUN TestMul2/pos
=== RUN TestMul2/neg
=== RUN TestMul2/zero
TestMul2/zero: calc_test.go:55: 0 * -2 expected 10, but 0 got
--- FAIL: TestMul2 (0.00s)
--- PASS: TestMul2/pos (0.00s)
--- PASS: TestMul2/neg (0.00s)
--- FAIL: TestMul2/zero (0.00s)
FAIL
exit status 1
FAIL org.golearn.basic/test 0.005s