t.Run可用于组织子测试,使go测试更清晰易读,支持独立命名、运行和并行执行子测试,常与表格驱动测试结合使用,提升维护性和调试效率。

在 Go 中使用 t.Run 可以很好地组织子测试(subtests),让测试更清晰、可读性更强,也便于调试和筛选运行特定用例。它特别适合对同一函数的不同输入场景进行分组测试。
使用 t.Run 创建子测试
*testing.T 提供了 Run 方法,可以创建嵌套的子测试。每个子测试有独立的名字,并能单独运行或失败,不会影响其他子测试的执行(除非设置了 t.Parallel())。
基本语法如下:
func TestFunction(t *testing.T) { t.Run(“case description”, func(t *testing.T) { // 子测试逻辑 }) }
示例:测试一个简单的加法函数:
立即学习“go语言免费学习笔记(深入)”;
func Add(a, b int) int { return a + b } func TestAdd(t *testing.T) { t.Run(“positive numbers”, func(t *testing.T) { if Add(2, 3) != 5 { t.Errorf(“expected 5, got %d”, Add(2,3)) } }) t.Run(“negative numbers”, func(t *testing.T) { if Add(-1, -1) != -2 { t.Errorf(“expected -2, got %d”, Add(-1,-1)) } }) t.Run(“zero values”, func(t *testing.T) { if Add(0, 0) != 0 { t.Errorf(“expected 0, got %d”, Add(0,0)) } }) }
使用表格驱动测试配合 t.Run
更常见的是结合表格驱动测试(table-driven tests)来批量生成子测试,结构清晰且易于扩展。
示例如下:
func TestAdd_TableDriven(t *testing.T) { tests := map[String]Struct{ a, b int want int }{ “positive”: {a: 2, b: 3, want: 5}, “negative”: {a: -1, b: -1, want: -2}, “with zero”: {a: 0, b: 0, want: 0}, “mixed”: {a: -1, b: 1, want: 0}, } for name, tc := range tests { t.Run(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) } }) } }
这种写法将所有测试用例集中管理,通过名称动态创建子测试,输出结果也会显示每个子测试的名称,方便定位问题。
子测试的优势与实用技巧
独立命名与运行:Go 支持通过 -run 标志运行指定子测试。比如:
go test -run TestAdd/positive
这只会运行 TestAdd 中名为 positive numbers 的子测试。
作用域隔离:每个子测试有自己的 *testing.T 实例,t.Parallel() 可以安全地在子测试中调用,实现并行执行。
例如:
t.Run(“parallel case”, func(t *testing.T) { t.Parallel() // 并行执行的测试逻辑 })
共享前置逻辑:可以在子测试外写公共 setup 或 teardown 逻辑,子测试内只关注具体断言。
基本上就这些。使用 t.Run 能让测试更有结构,尤其是配合表格驱动模式后,维护性和可读性大幅提升。


