我想测试yaml的解析并通过单元测试进行测试 我创建了具有适当类型的结构,但断言总是如此 愚蠢的,我尝试使用以下代码,不断失败
这是yaml内容有效(可能是副本已更改,但我能够正确解析它)
ID: demo
version: 0.0.5
dep:
- name: db
path: mtb
requires:
- name: vi_db
- name: srv
path: srv1
properties:
LOG_LEVEL: "info"
parameters:
mem: 12G
requires:
- name: db
properties:
这是我创建的测试
func Test_parseFile(t *testing.T) {
yamlfile, err := ioutil.ReadFile("./testdata/file.yaml")
type Properties map[string]string
type Parameters map[string]interface{}
type Modules struct {
Name string
Path string `yaml:"path,omitempty"`
Requires []Requires `yaml:"requires,omitempty"`
Properties Properties `yaml:"properties,omitempty"`
}
type Requires struct {
Name string `yaml:"name,omitempty"`
Properties Properties `yaml:"properties,omitempty"`
}
type args struct {
contentFile []byte
}
tests := []struct {
name string
args args
wantOut Properties
wantNoTests bool
wantErr bool
}{
{
name: "test",
args: args{
contentFile: yamlfile,
},
wantOut: Modules{
Name: "srv",
Path: "srv1",
Properties{
"LOG_LEVEL": "info",
"DEBUG_LOG_LEVEL": "ALL",
},
Parameters:{
"mem":"12G",
},
Requires: {
name: "db",
Properties{
"CONFIG": '[tomcontext.xml:
{"service_nameDB" : "~{con-name}"}]'
},
},
},
wantNoTests: true,
wantErr: true,
},
}
这是断言代码
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOut := ParseFile(tt.args.contentFile)
if !reflect.DeepEqual(gotOut.Modules[1], tt.wantOut) {
t.Errorf("parseFile() = %v, want %v", gotOut.Modules[2], tt.wantOut)
}
错误是:
parseFile() = map[], want map[LOG_LEVEL:info DEBUG_LOG_LEVEL:ALL]
我应该如何克服它来检查模块属性?
ParseFile
方法只是err := yaml.Unmarshal([]byte(yamlFile), &yamlconent)
答案 0 :(得分:5)
我不完全确定问题是什么,但我设法让你的测试像这样工作:
package sandbox
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
type Properties map[string]string
type Parameters map[string]interface{}
type Requires struct {
Name string `yaml:"name,omitempty"`
Properties Properties `yaml:"properties,omitempty"`
}
type Module struct {
Name string
Path string `yaml:"path,omitempty"`
Requires []Requires `yaml:"requires,omitempty"`
Properties Properties `yaml:"properties,omitempty"`
Parameters Parameters `yaml:"parameters,omitempty"`
}
type File struct {
Modules []Module
}
func Test_ParseFile(t *testing.T) {
input := []byte(`ID: demo
version: 0.0.5
modules:
- name: db
path: mtb
requires:
- name: vi_db
- name: srv
path: srv1
properties:
LOG_LEVEL: "info"
DEBUG_LOG_LEVEL : ALL
parameters:
mem: 12G
requires:
- name: db
properties:
CONFIG: '[tomcontext.xml:
{"service_nameDB" : "~{con-name}"}]'`)
tests := []struct {
name string
wantOut Module
}{
{
name: "test",
wantOut: Module{
Name: "srv",
Path: "srv1",
Properties: Properties{
"LOG_LEVEL": "info",
"DEBUG_LOG_LEVEL": "ALL",
},
Parameters: Parameters{
"mem": "12G",
},
Requires: []Requires{
{
Name: "db",
Properties: Properties{
"CONFIG": `[tomcontext.xml: {"service_nameDB" : "~{con-name}"}]`,
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := ParseFile(input)
require.NoError(t, err)
require.NotNil(t, actual)
require.Len(t, actual.Modules, 2)
assert.Equal(t, tt.wantOut, actual.Modules[1])
})
}
}
func ParseFile(yamlFile []byte) (File, error) {
var f File
err := yaml.Unmarshal(yamlFile, &f)
return f, err
}
请注意,我导入了https://github.com/stretchr/testify以使测试更容易一些。当然,您可以将其替换为原始的reflect.DeepEquals
支票。 Testify在这里很有用,因为它会在不满足期望的情况下打印出有用的差异。