【发布时间】:2021-12-26 13:17:38
【问题描述】:
我有一个问题,我需要将两个非常大的结构(生成的 protobuf)相互比较,作为测试用例的一部分。这些结构中有多个嵌套数组。下面是重现/演示问题的简化示例。
package pkg
import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
type structOne struct {
Foo string
Offs []*structTwo
}
type structTwo struct {
Identifier string
}
func Test_Compare(t *testing.T) {
exp := &structOne{
Foo: "bar",
Offs: []*structTwo{
{
Identifier: "one",
},
{
Identifier: "two",
},
{
Identifier: "three",
},
{
Identifier: "four",
},
},
}
act := &structOne{
Foo: "bar",
Offs: []*structTwo{
{
Identifier: "four",
},
{
Identifier: "three",
},
{
Identifier: "two",
},
{
Identifier: "one",
},
},
}
assert.Equal(t, exp, act) // fails
assert.True(t, reflect.DeepEqual(exp, act)) // fails
}
我尝试过使用assert.Equal(t, exp, act) 和assert.True(t, reflect.DeepEqual(exp, act))。我正在寻找一种比较此类结构的方法,最好不必为所有对象创建自定义比较函数。
谢谢
【问题讨论】:
标签: unit-testing go