douqiao8032 2018-07-30 18:30
浏览 1
已采纳

表驱动的文件创建测试

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, "
")
        fmt.Fprint(file, "
")
        fmt.Fprint(file,  appStr.vtype) //"userApp"
        fmt.Fprint(file, "
")
        fmt.Fprint(file, appStr.name) //"applicationValue"
        fmt.Fprint(file, "
")
        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")
            }
        })
    }
}

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

  • 写回答

1条回答 默认 最新

  • dqkelut8423 2018-07-31 15:53
    关注

    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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口