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

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

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题