package add import "testing" func TestAdd(t *testing.T) { // 1. 定义测试表:每行是 输入a, 输入b, 期望结果want tests := []struct { a, b, want int name string // 给用例起个可读名字,失败时好定位 }{ {2, 3, 5, "positive"}, {-1, -1, -2, "negative"}, {0, 7, 7, "zero"}, {0, 0, 0, "zero+zero"}, {-5, 5, 0, "negative+positive"}, } // 2. 遍历表格,一行就是一个子测试 for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { // 子测试,失败时能看到名字 got := Add(tc.a, tc.b) if got != tc.want { t.Errorf("Add(%d, %d) = %d; want %d", tc.a, tc.b, got, tc.want) } }) } }