表驱动的文件创建测试 [英] Table-driven test for file creation

查看:36
本文介绍了表驱动的文件创建测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从@volker得到了一个有关表驱动测试的示例,如下所示但是目前我错过了我应该在真实测试中添加的内容,该测试使用的是字节,目前尚不确定要在 args expected [] byte 中放入什么,例如我想检查文件中是否有 2新行,然后是 application 条目,我该怎么做而无需创建真实文件并进行解析?

I got an example from @volker about table driven test like following But currently I miss what I should put in the real test, this test is using byte, currently im not sure what to put in the args and the expected []byte, e.g. I want to check that in the file there is 2 new line and then application entry, how can I do it without the need to create the real file and parse it?

type Models struct {
    name        string
    vtype       string
    contentType string
}

func setFile(file io.Writer, appStr Models) {
    fmt.Fprint(file, "1.0")

    fmt.Fprint(file, "Created-By: application generation process")
    for _, mod := range appStr.Modules {
        fmt.Fprint(file, "\n")
        fmt.Fprint(file, "\n")
        fmt.Fprint(file,  appStr.vtype) //"userApp"
        fmt.Fprint(file, "\n")
        fmt.Fprint(file, appStr.name) //"applicationValue"
        fmt.Fprint(file, "\n")
        fmt.Fprint(file, appStr.contentType)//"ContentType"
    }
}

func Test_setFile(t *testing.T) {
    type args struct {
        appStr models.App
    }
    var tests []struct {
        name string
        args args
        expected []byte
   }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            b := &bytes.Buffer{}
            setFile(b, tt.args.AppStr)
            if !bytes.Equal(b.Bytes(), tt.expected) {
                t.Error("somewhat bad happen")
            }
        })
    }
}

我阅读并理解了以下示例,但不了解字节和文件 https://medium.com/@virup/如何编写简洁的测试表驱动测试ed672c502ae4

I read and understand the following example but not for byte and file https://medium.com/@virup/how-to-write-concise-tests-table-driven-tests-ed672c502ae4

推荐答案

如果仅在开始时检查静态内容,则实际上只需要进行一项测试.看起来像这样:

If you are only checking for the static content at the beginning, then you really only need one test. It would look something like this:

func Test_setFile(t *testing.T) {
    type args struct {
        appStr models.App
    }
    var tests []struct {
        name string
        args args
        expected []byte
    }{
        name: 'Test Static Content',
        args: args{appStr: 'Some String'},
        expected: []byte(fmt.Sprintf("%s%s%s", NEW_LINE, NEW_LINE, "Application")),
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            b := &bytes.Buffer{}
            setFile(b, tt.args.AppStr)
            if !bytes.Equal(b.Bytes(), tt.expected) {
                t.Error("somewhat bad happen")
            }
        })
    }
}

尽管,由于此测试只有一个案例,所以这里确实不需要使用表驱动的测试.您可以清理它,使其看起来像这样:

Although, since you only have one case for this test, there really isn't a need to use table driven tests here. You could clean it up to look something like this:

func Test_setFile(t *testing.T) {
    b := &bytes.Buffer{}
    setFile(b, 'Some String')
    want := []byte(fmt.Sprintf("%s%s%s", NEW_LINE, NEW_LINE, "Application"))
    got := b.Bytes()
    if !bytes.Equal(want, got) {
        t.Errorf("want: %s got: %s", want, got)
    }
}

这篇关于表驱动的文件创建测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆