← Back
后端开发 2026.03.06

Go Senior Engineer Explains (MOOC) 003

后端开发

Testing

Complaining about others’ approaches, the Go language uses table-driven tests.

  • Test data and test logic are mixed together
  • Unclear error messages
  • All tests end once one data point is incorrect

Table-driven tests

test:=[]struct{
    a,b,c int32
}{
    {1,2,3},
    {0,2,0},
    {0,0,0},
    {0,0,0},
    {-1,1,0},
    {math.MaxInt32,1,math.MinInt32},
}
for _,test:= reange tests {
    if actual:=add(test.a,test.b); actual!=test.c{
        // 测试提示
    }
}
  • Separate test data and test logic
  • Clear error messages
  • Can fail partially