前言
在 golang 语言中,写单元测试的时候,不可避免的会涉及到对其他函数及方法的 mock,即在假设其他函数及方法响应预期结果的同时,校验被测函数的响应是否符合预期。
其中,在 mock 其他函数及方法的时候,我们常用到的一个测试类库是「gomonkey」。特别地,对于方法和函数的 mock,略有差异,在这里我们就分别给出函数和方法 mock 示例,方便大家参考。
函数
在 golang 语言中,函数是没有接受者的方法,其形式为
func function_name([parameter list]) [return_types] {
函数体
}
对于函数的 mock 相对来说比较简单,假设我们对 a 函数进行单元测试,且 a 函数里面又调用了 b 函数,例如
func a(ctx context.context, str string) error {
if len(str) == 0 {
return errors.new("str is empty")
}
return test_package_name.b(ctx, str)
}
为了将 a 函数的每一行代码都覆盖到,则其单元测试可以写为:
func testa(t *testing.t) {
type args struct {
ctx context.context
str string
}
tests := []struct {
name string
args args
setup func(t *testing.t)
wanterr error
}{
{
name: "len(str) == 0",
wanterr: errors.new("str is empty")
},
{
name: "正常响应",
setup: func(t *testing.t) {
patches := gomonkey.applyfunc(test_package_name.b, func(_ context.context, _ string) error {
return nil
})
t.cleanup(func() {
patches.reset()
})
},
args: args{
ctx: context.background(),
str: "test",
},
wanterr: nil,
},
}
// 执行测试用例
for _, tt := range tests {
t.run(tt.name, func(t *testing.t) {
if tt.setup != nil {
tt.setup(t)
}
err := a(tt.args.ctx, tt.args.str)
if err != nil {
assert.equalerror(t, err, tt.wanterr.error(), "error 不符合预期")
}
})
}
}
applyfuncsetupcleanuppatchesreset
方法
在 golang 语言中,方法是含有接受者的函数,其形式为
func (variable_name variable_data_type) function_name([parameter list]) [return_type]{
函数体
}
对于方法的 mock 相对来说复杂一下,假设我们对 a 函数进行单元测试,且 a 函数里面又调用了结构 c 的 b 方法,例如
func a(ctx context.context, str string) error {
if len(str) == 0 {
return errors.new("str is empty")
}
c := &test_package_name.c{}
return c.b(ctx, str)
}
为了将 a 函数的每一行代码都覆盖到,则其单元测试可以写为:
func testa(t *testing.t) {
// 初始化c结构
var c *test_package_name.c
type args struct {
ctx context.context
str string
}
tests := []struct {
name string
args args
setup func(t *testing.t)
wanterr error
}{
{
name: "len(str) == 0",
wanterr: errors.new("str is empty")
},
{
name: "正常响应",
setup: func(t *testing.t) {
patches := gomonkey.applymethod(reflect.typeof(c), "b", func(_ *test_package_name.c, _ context.context, _ string) error {
return nil
})
t.cleanup(func() {
patches.reset()
})
},
args: args{
ctx: context.background(),
str: "test",
},
wanterr: nil,
},
}
// 执行测试用例
for _, tt := range tests {
t.run(tt.name, func(t *testing.t) {
if tt.setup != nil {
tt.setup(t)
}
err := a(tt.args.ctx, tt.args.str)
if err != nil {
assert.equalerror(t, err, tt.wanterr.error(), "error 不符合预期")
}
})
}
}
applymethod
reflect.typeofbbfunc(ctx context.context, str string)applymethodbfunc(c *test_package_name.c, ctx context.context, str string)
参考
gomonkey
golang使用gomonkey和monkey来mock方法或者函数时报panic: permission denied
使用 gomonkey 遇到非 debug 模式执行失败的问题及解决方法
到这里,本文就要结束了,希望对大家有所帮助。