gpt4 book ai didi

go - 具有结构的golang单元测试

转载 作者:数据小太阳 更新时间:2023-10-29 03:03:24 26 4
gpt4 key购买 nike

我使用 VSCode 生成我项目的测试文件,

当前它生成以下结构

tests := []struct {
name string
args args
wantOut ZTR
}{
name: "test123",
args: args{
ztrFile: "./testdata/ztrfile.yaml",
},
wantOut: “ZTR.Modules",
}

测试应该包括yaml的解析和属性测试

这里调用解析文件

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOut := parseFile(tt.args.ztrFile); !reflect.DeepEqual(gotOut, tt.wantOut) {
t.Errorf("parseFile() = %v, want %v", gotOut, tt.wantOut)
}
})

这是结构

type Modules struct {
Name string
Type string
cwd string `yaml:”cwd,omitempty"`
}

不确定我需要在此处放置什么才能使其正常工作,我尝试使用这些类型但出现错误

        {
name: "test123",
args: args{
mtaFile: "./testdata/ztrfile.yaml",
},
wantOut: “ZTR.Modules",
}

我得到的错误是

message: '不能使用“test123”(字符串类型)作为结构类型 { name string;参数参数; wantOut ZTR } 在数组或 slice 文字中'在:'41,3'资源: ''代码:'未定义'

最佳答案

您的 tests 声明不正确。您需要提供一片结构,但您只提供键/值:

tests := []struct {
name string
args args
wantOut ZTR
}{
name: "test123",
args: args{
mtaFile: "./testdata/ztrfile.yaml",
},
wantOut: “ZTR.Modules",
}

应该是:

tests := []struct {
name string
args args
wantOut ZTR
}{
{
name: "test123",
args: args{
mtaFile: "./testdata/ztrfile.yaml",
},
wantOut: “ZTR.Modules",
},
}

关于go - 具有结构的golang单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49557658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com