起一个docker环境来测试,测试完remove

安装go语言的Docker客户端

1
go get github.com/docker/docker/client

使用docker并自动remove掉

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
func main() {
c, err := client.NewEnvClient()
if err != nil {
panic(err)
}

ctx := context.Background()

resp, err := c.ContainerCreate(ctx, &container.Config{
Image: "mongo",
ExposedPorts: nat.PortSet{
"27017/tcp": {},
},
}, &container.HostConfig{
PortBindings: nat.PortMap{
"27017/tcp": []nat.PortBinding{
{
HostIP: "127.0.0.1",
HostPort: "0", //表示自动选择端口
},
},
},
}, nil, nil, "")
if err != nil {
panic(err)
}
err = c.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
panic(err)
}

fmt.Println("container started")

time.Sleep(5 * time.Second)
inspRes, err := c.ContainerInspect(ctx, resp.ID)
if err != nil {
panic(err)
}

fmt.Printf("listening at %+v\n",
inspRes.NetworkSettings.Ports["27017/tcp"][0])
err = c.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{
Force: true,
})
if err != nil {
panic(err)
}
fmt.Println("remove container\n")
}

image-20220302154809201

建立针对MongoDB的测试库

每个测试都要启动一个docker还是不行

步骤1:建立shared/mongo/testing/mongotesting.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
const (
image = "mongo"
containerPort = "27017/tcp"
)

func RunWithMongoInDocker(m *testing.M, mongoURI *string) int {
c, err := client.NewEnvClient()
if err != nil {
panic(err)
}

ctx := context.Background()

resp, err := c.ContainerCreate(ctx, &container.Config{
Image: image,
ExposedPorts: nat.PortSet{
containerPort: {},
},
}, &container.HostConfig{
PortBindings: nat.PortMap{
containerPort: []nat.PortBinding{
{
HostIP: "127.0.0.1",
HostPort: "0", //表示自动选择端口
},
},
},
}, nil, nil, "")
if err != nil {
panic(err)
}
err = c.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
panic(err)
}

fmt.Println("container started")

inspRes, err := c.ContainerInspect(ctx, resp.ID)
if err != nil {
panic(err)
}
pb := inspRes.NetworkSettings.Ports["27017/tcp"][0]
*mongoURI = fmt.Sprintf("mongodb://%s:%s", pb.HostIP, pb.HostPort)
defer func() {
err = c.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{
Force: true,
})
if err != nil {
panic(err)
}
}()
return m.Run()
}

步骤2:auth/auth/dao/mongo_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
var mongoURI string

func TestResolverAccountID(t *testing.T) {
c := context.Background()
s := mongoURI
mc, err := mongo.Connect(c, options.Client().ApplyURI(s))
if err != nil {
t.Fatalf("cannot connection db: %v", err)
}
m := NewMongo(mc.Database("coolcar"))
id, err := m.ResolverAccountID(c, "123")
if err != nil {
t.Errorf("faild resolved account id 123: %v", err)
} else {
want := "621ecd8d42ffa4ec7cf1a22c"
if id != want {
t.Errorf("resolve account id : want: %q, got: %q", want, id)
}
}

}

func TestMain(m *testing.M) {
os.Exit(mongotesting.RunWithMongoInDocker(m, &mongoURI))
}

启动测试

表格驱动测试实践

将上面代码整合成下面这样,表格驱动测试核心就是for循环测试用例。测试数据一目了然

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
var mongoURI string

func TestResolverAccountID(t *testing.T) {
c := context.Background()
s := mongoURI
mc, err := mongo.Connect(c, options.Client().ApplyURI(s))
if err != nil {
t.Fatalf("cannot connection db: %v", err)
}
m := NewMongo(mc.Database("coolcar"))
m.col.InsertMany(c, []interface{}{
bson.M{
"_id": mustObjID("621ecd8d42ffa4ec7cf1a22c"),
"open_id": "open_1",
},
bson.M{
"_id": mustObjID("621ecd8d42ffa4ec7cf1a23c"),
"open_id": "open_2",
},
bson.M{
"_id": mustObjID("621ecd8d42ffa4ec7cf1a24c"),
"open_id": "open_3",
},
})
// m.newObjID = func() primitive.ObjectID {
// return mustObjID("621ecd8d42ffa4ec7cf1a26c")
// }
cases := []struct {
name string
openID string
want string
}{
{
name: "existing_user",
openID: "open_1",
want: "621ecd8d42ffa4ec7cf1a22c",
},
{
name: "another_existing_user",
openID: "open_2",
want: "621ecd8d42ffa4ec7cf1a23c",
},
{
name: "new_user",
openID: "open_3",
want: "621ecd8d42ffa4ec7cf1a24c",
},
}
for _, cc := range cases {
t.Run(cc.name, func(t *testing.T) {
id, err := m.ResolverAccountID(context.Background(), cc.openID)
if err != nil {
t.Errorf("faild resolved account id %s: %v", cc.openID, err)
}
if id != cc.want {
t.Errorf("resolve account id : want: %q, got: %q", cc.want, id)
}
})
}
id, err := m.ResolverAccountID(c, "123")
if err != nil {
t.Errorf("faild resolved account id 123: %v", err)
} else {
want := "621ecd8d42ffa4ec7cf1a22c"
if id != want {
t.Errorf("resolve account id : want: %q, got: %q", want, id)
}
}

}

func mustObjID(hex string) primitive.ObjectID {
oi, _ := primitive.ObjectIDFromHex(hex)
return oi
}

func TestMain(m *testing.M) {
os.Exit(mongotesting.RunWithMongoInDocker(m, &mongoURI))
}