[英]How to test a collection of functions by reflection in Go?
I have to write unit tests for several functions with similar signature and return values (an object and an error), which must pass similar test conditions. 我必须为具有类似签名和返回值(对象和错误)的几个函数编写单元测试,这些函数必须通过类似的测试条件。
I would like to avoid writing: 我想避免写作:
func TestFunc1(t *testing.T) {
// tests on return values
}
func TestFunc2(t *testing.T) {
// tests identical for Func1
}
func TestFunc3(t *testing.T) {
// tests identical for Func1
}
...
(See this go playground example for a more complete context) (有关更完整的上下文,请参阅此操作示例 )
(yes, go playground doesn't support yet go test
, only go run
, and issue 6511 is there to request that feature) (是的,去游乐场不支持go test
,只go run
,并且问题6511是请求该功能)
How would you use reflection ( reflect
package ) in order to write only one test which would: 你如何使用反射( reflect
包 )才能只编写一个测试:
I have seen: 我见过:
.Call
in reflect package, Golang? ", using Value.Call “ 如何正确使用.Call
in reflect package,Golang? ”,使用Value.Call But I miss a complete example for calling functions and using the returned values in a test. 但我想念一个完整的例子来调用函数并在测试中使用返回的值。
Once I understood that everything must use or return the type Value , here is what I came up with. 一旦我理解了所有必须使用或返回类型值 ,这就是我想出的。
The trick is to use: 诀窍是使用:
ValueOf
in order to get a value of the receiver ValueOf
以获取接收器的值 Value.MethodByName
to find a function of that receiver value Value.MethodByName
用于查找该接收者值的函数 Value.IsNil
to test for nil
returned value. Value.IsNil
测试nil
返回值。 Main extract of the test code: 测试代码的主要摘录:
var funcNames = []string{"Func1", "Func2", "Func3"}
func TestFunc(t *testing.T) {
stype := reflect.ValueOf(s)
for _, fname := range funcNames {
fmt.Println(fname)
sfunc := stype.MethodByName(fname)
// no parameter => empty slice of Value
ret := sfunc.Call([]reflect.Value{})
val := ret[0].Int()
// That would panic for a nil returned err
// err := ret[1].Interface().(error)
err := ret[1]
if val < 1 {
t.Error(fname + " should return positive value")
}
if err.IsNil() == false {
t.Error(fname + " shouldn't err")
}
}
}
See a runnable example in go playground . 在go playground中查看runnable示例 。
Note that if you are calling that test function with a non-existent function name, that will panic. 请注意,如果您使用不存在的函数名称调用该测试函数,则会发生混乱。
See that example here . 在这里看到这个例子 。
runtime.panic(0x126660, 0x10533140)
/tmp/sandbox/go/src/pkg/runtime/panic.c:266 +0xe0
testing.func·005()
/tmp/sandbox/go/src/pkg/testing/testing.go:383 +0x180
----- stack segment boundary -----
runtime.panic(0x126660, 0x10533140)
/tmp/sandbox/go/src/pkg/runtime/panic.c:248 +0x160
reflect.flag.mustBe(0x0, 0x13)
/tmp/sandbox/go/src/pkg/reflect/value.go:249 +0xc0
reflect.Value.Call(0x0, 0x0, 0x0, 0xfeef9f28, 0x0, ...)
/tmp/sandbox/go/src/pkg/reflect/value.go:351 +0x40
main.TestFunc(0x10546120, 0xe)
/tmpfs/gosandbox-3642d986_9569fcc1_f443bbfb_73e4528d_c874f1af/prog.go:34 +0x240
Go playground recover from that panic, but your test program might not. 操场从恐慌中恢复,但你的测试程序可能没有。
That is why I added to the test function above: 这就是为什么我添加到上面的测试函数:
for _, fname := range funcNames {
defer func() {
if x := recover(); x != nil {
t.Error("TestFunc paniced for", fname, ": ", x)
}
}()
fmt.Println(fname)
That produces ( see example ) a much nicer output: 这会产生( 参见示例 )更好的输出:
Func1
Func2
Func3
Func4
--- FAIL: TestFunc (0.00 seconds)
prog.go:48: Func2 should return positive value
prog.go:51: Func3 shouldn't err
prog.go:32: TestFunc paniced for Func4 : reflect: call of reflect.Value.Call on zero Value
FAIL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.