共计 706 个字符,预计需要花费 2 分钟才能阅读完成。
func check(a, b int) bool {return false}
用 goland 给上面这个函数生成的测试方法
func Test_check(t *testing.T) {
type args struct {
a int
b int
}
tests := []struct {
name string
args args
want bool
}{// TODO: Add test cases.}
for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {if got := check(tt.args.a, tt.args.b); got != tt.want {t.Errorf("check() = %v, want %v", got, tt.want)
}
})
}
}
默认生成的规则是把函数所有的参数放到一个 args
结构体里面。自己不太喜欢这种风格。
我希望是下面这种风格。
func Test_check(t *testing.T) {tests := []struct {
name string
a int
b int
want bool
}{// TODO: Add test cases.}
for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {if got := check(tt.a, ttb); got != tt.want {t.Errorf("check() = %v, want %v", got, tt.want)
}
})
}
}
请教一下 goland 是否可以改成默认用我这种风格。
顺便再请教一个问题:
- 如果一个函数的参数有一个大的结构体(比如 k8s 的 deployment 那种),每次写单元测试的时候,要构造一个大的结构体,特别麻烦,有没有好的解决方案。
正文完