dongqian6484 2017-05-22 17:38
浏览 7
已采纳

改变结构域进行单元测试

I'm trying to unit test a constructor for a struct with many fields. I want to make sure that the constructor performs all the expected validations so I am testing single fields for multiple failure scenarios.

I'm trying to do this programatically so I'm using table tests, however this leads to a lot of repetition and noise in the tests as I end up repeating N param fields just to test for the one field error.

For example:

func NewSomeObject(p *Params) *SomeObject {
  ...
}


type SomeObject struct {
  ..
  Field1 string
  Field2 string
  Field3 string
  ...
  Field10 string
}

func TestNewSomeObject(t *testing.T) {
    tcases := map[string]struct {
        params *Params
        err    error
    }{
        &Params{
          Field1: "invalid_0" // <--- making sure that this invalid field is caught
          Field2: "valid"
          Field3: "valid"
          ...
          Field10: "valid"
        },
        &Params{
          Field1: "invalid_1" // <--- another invalid case
          Field2: "valid"
          Field3: "valid"
          ...
          Field10: "valid"
        },
        &Params{
          Field1: "invalid_2"
          Field2: "valid"
          Field3: "valid"
          ...
          Field10: "valid"
        },
        &Params{
          Field1: "invalid_3"
          Field2: "valid"
          Field3: "valid"
          ...
          Field10: "valid"
        },
        ...
        ...
        ...
    }  

    for name, tc := range tcases {
        t.Logf("Running test %s", name)

        s, err := NewSomeObject(tc.params)
        if !reflect.DeepEqual(tc.err, err) {
            t.Fatalf("Got '%v', Expected: '%v'", err, tc.err)
        }
    }
}

Is there a better way to vary a single field in the struct without having to repeat the input so many times?

  • 写回答

1条回答 默认 最新

  • dongli1920 2017-05-22 18:27
    关注

    You can avoid repetitive code by creating one constructor that would set up all default values (valid).

    The constructor can also receive a function to operate over the created object before returning it.

    That way, you only need to code the logic required to invalidate the particular field you'd like to test.

    To create a Params object you can just do:

    params1 := CreateParamsWith(func(p *Params) {
        p.Field1 = "invalid_0"
    })
    

    The CreateParamsWith might look like this:

    func CreateParamsWith(modifyParams func(*Params)) (*Params) {
        params := &Params{  
            Field1: "valid",
            Field2: "valid",
            Field3: "valid",
            Field4: "valid",
            Field5: "valid",
            }
        modifyParams(params)
        return params
    }
    

    Full working code here: https://play.golang.org/p/U0xhtIbQfy

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?