答案:Go中结合泛型与表格驱动测试可提升代码复用和类型安全,通过泛型断言函数assertEqual和通用测试结构体testFindIndexGeneric,减少重复逻辑,支持多类型验证,使单元测试更简洁高效。

在 Go 语言中,表格驱动测试(Table-Driven Tests)是编写单元测试的常见模式。结合泛型,可以构建更通用、类型安全的测试逻辑,减少重复代码。下面是一个将泛型与表格驱动测试结合使用的实用示例。
我们定义一个泛型辅助函数来比较期望值和实际值,避免每个测试用例都写重复的判断逻辑。
func assertEqual[T comparable](t *testing.T, name string, got, want T) {
t.Helper()
if got != want {
t.Errorf("[%s] expected: %v, got: %v", name, want, got)
}
}
这个 assertEqual 函数适用于任何可比较类型(如 int、string、struct 等),让测试更简洁。
假设我们有一个泛型查找函数 FindIndex,它在切片中查找满足条件的第一个元素索引:
立即学习“go语言免费学习笔记(深入)”;
func FindIndex[T any](slice []T, predicate func(T) bool) int {
for i, v := range slice {
if predicate(v) {
return i
}
}
return -1
}
我们可以为它编写表格驱动测试,覆盖多种类型场景:
func TestFindIndex(t *testing.T) {
tests := []struct {
name string
slice interface{}
pred interface{}
want int
}{
{
name: "int: 找到偶数",
slice: []int{1, 3, 4, 5},
pred: func(x int) bool { return x%2 == 0 },
want: 2,
},
{
name: "string: 找到空字符串",
slice: []string{"a", "", "b"},
pred: func(s string) bool { return s == "" },
want: 1,
},
{
name: "struct: 找到特定字段",
slice: []Person{{"Alice", 25}, {"Bob", 30}},
pred: func(p Person) bool { return p.Name == "Bob" },
want: 1,
},
{
name: "未找到",
slice: []int{1, 2, 3},
pred: func(x int) bool { return x > 10 },
want: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
switch slice := tt.slice.(type) {
case []int:
pred := tt.pred.(func(int) bool)
got := FindIndex(slice, pred)
assertEqual(t, tt.name, got, tt.want)
case []string:
pred := tt.pred.(func(string) bool)
got := FindIndex(slice, pred)
assertEqual(t, tt.name, got, tt.want)
case []Person:
pred := tt.pred.(func(Person) bool)
got := FindIndex(slice, pred)
assertEqual(t, tt.name, got, tt.want)
}
})
}
}
type Person struct {
Name string
Age int
}
虽然这里用了 interface{} 存储不同类型,但通过类型断言确保类型安全。测试结构清晰,易于扩展新类型。
如果只测试单一类型,可以直接使用泛型结构体,避免类型断言:
func testFindIndexGeneric[T comparable](t *testing.T, name string, slice []T, pred func(T) bool, want int) {
t.Run(name, func(t *testing.T) {
got := FindIndex(slice, pred)
assertEqual(t, name, got, want)
})
}
func TestFindIndex_GenericHelper(t *testing.T) {
testFindIndexGeneric(t, "整数查找", []int{10, 20, 30}, func(x int) bool { return x > 15 }, 1)
testFindIndexGeneric(t, "字符串查找", []string{"go", "rust", "ts"}, func(s string) bool { return s == "rust" }, 1)
}
这种方式更安全、更简洁,适合类型明确的测试场景。
基本上就这些。泛型 + 表格驱动能让 Go 测试更灵活、少冗余,关键是设计好可复用的辅助函数和结构。不复杂但容易忽略细节类型匹配。
以上就是Golang测试表格驱动与泛型结合示例的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号