Go Senior Engineer Explains (MOOC) 003

Table of Contents

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

主题测试文章,只做测试使用。发布者:Walker,转转请注明出处:https://walker-learn.xyz/archives/6738

(0)
Walker的头像Walker
上一篇 13 hours ago
下一篇 Feb 26, 2025 17:52

Related Posts

EN
简体中文 繁體中文 English