如何在Golang中使用testing.TB接口统一测试_支持Test和Benchmark

使用 testing.TB 接口可让同一逻辑函数同时支持测试和基准测试,避免校验逻辑重复与不一致;定义接收 testing.TB 参数的函数,在 Test 和 Benchmark 中分别传入 testing.T 或 testing.B 即可复用。

在 Go 中,testing.TB*testing.T(用于测试)和 *testing.B(用于基准测试)共同实现的接口,它抽象了日志、失败、跳过等基础行为。利用这个接口,你可以编写**一份逻辑代码,同时支持 TestBenchmark 函数**,避免重复实现核心测试逻辑。

为什么用 testing.TB 而不是分别写两个函数?

当你要验证某段逻辑的正确性(如算法输出)并同时衡量其性能时,验证逻辑往往完全一致——比如“输入 X 应该返回 Y”。若分开写 TestXXXBenchmarkXXX,容易出现:校验条件不一致、修复 bug 时只改了一个函数、新增 case 需同步两处。用 testing.TB 抽象后,核心断言和流程只写一次,提升可维护性与一致性。

如何定义一个接收 testing.TB 的通用函数?

定义一个函数,参数为 testing.TB,内部用其方法做日志、失败、跳过等操作。注意:不能直接调用 t.Fatal 后继续执行(会 panic),但可以安全使用 t.Error/t.Errorf

示例:

func runMyLogic(t testing.TB, input int) int {
    result := expensiveComputation(input)
    expected := input * 2
    if result != expected {
        t.Errorf("expensiveComputation(%d) = %d, want %d", input, result, expected)
    }
    return result
}

在 Test 和 Benchmark 中复用该函数

只需将 *testing.T*testing.B 传入即可。它们都实现了 testing.TB 接口。

  • 测试函数中调用:
func TestExpensiveComputation(t *testing.T) {
    runMyLogic(t, 5)
}
  • 基准测试函数中调用(注意循环调用):
func BenchmarkExpensiveComputation(b *testing.B) {
    b.ReportAllocs()
    for i := 0; i < b.N; i++ {
        runMyLogic(b, 5) // ✅ 合法:*testing.B 实现了 testing.TB
    }
}

⚠️ 注意:基准测试中不要在循环外做校验(如只校验一次),否则结果不准;但 runMyLogic 内部的 t.Errorf*testing.B 上调用是安全的——它只会记录错误,不会终止基准循环。

进阶:支持子测试 + 子基准(Table-Driven)

结合 t.Runb.Run 可进一步统一数据驱动测试。由于两者签名不同(t.Run(string, func(*testing.T)) vs b.Run(string, func(*testing.B))),需稍作适配:

func TestAndBenchmarkTable(t testing.TB) {
    tests := []struct{
        name string
        input int
        want  int
    }{
        {"small", 2, 4},
        {"large", 100, 200},
    }
for _, tt := range tests {
    // 匿名函数闭包捕获 tt,适配 TB
    fn := func(tb testing.TB) {
        result := expensiveComputation(tt.input)
        if result != tt.want {
            tb.Errorf("got %d, want %d", result, tt.want)
        }
    }

    if t, ok := t.(*testing.T); ok {
        t.Run(tt.name, func(t *testing.T) { fn(t) })
    } else if b, ok := t.(*testing.B); ok {
        b.Run(tt.name, func(b *testing.B) { fn(b) })
    }
}

}

func TestExpensiveComputationTable(t *testing.T) { TestAndBenchmarkTable(t) }

func BenchmarkExpensiveComputationTable(b *testing.B) { TestAndBenchmarkTable(b) }

这样既保持单点逻辑,又支持清晰的子项分组和独立计时/失败报告。