使用testify时,表格驱动测试总是显示父测试失败
使用testify进行表格驱动测试时的子测试定位问题
问题场景
当使用testify编写带子测试的表格驱动测试时,若复用父测试的assert实例,会导致失败结果无法显示具体子测试名称,难以定位问题。
问题代码
func TestFoo(t *testing.T) { a := assert.New(t) tc := []struct { desc string foo string }{ { desc: "fail abc", foo: "abc", }, { desc: "fail def", foo: "def", }, } for _, tC := range tc { t.Run(tC.desc, func(t *testing.T) { tC := tC a.Equal(tC.foo, "ghi") }) } }
错误的失败输出
--- FAIL: TestFoo (0.00s) /test.go:27: Error Trace: /test.go:27 Error: Not equal: expected: "abc" actual : "ghi" Diff: --- Expected +++ Actual @@ -1 +1 @@ -abc +ghi Test: TestFoo /test.go:27: Error Trace: /test.go:27 Error: Not equal: expected: "def" actual : "ghi" Diff: --- Expected +++ Actual @@ -1 +1 @@ -def +ghi Test: TestFoo FAIL
所有失败都显示关联父测试TestFoo,而非具体的子测试(如TestFoo/fail_abc),用例越多定位越困难。
问题原因
核心问题是复用了父测试的assert实例:a := assert.New(t)在父测试中创建,绑定的是父测试的*testing.T对象。即使在子测试中调用该实例的断言方法,报错时仍会关联父测试的上下文,无法识别子测试的名称。
解决方案
需要让断言方法绑定子测试的*testing.T对象,有两种常用写法:
方案1:在子测试内创建新的assert实例
func TestFoo(t *testing.T) { tc := []struct { desc string foo string }{ { desc: "fail abc", foo: "abc", }, { desc: "fail def", foo: "def", }, } for _, tC := range tc { t.Run(tC.desc, func(t *testing.T) { tC := tC a := assert.New(t) // 绑定子测试的t对象 a.Equal(tC.foo, "ghi") }) } }
方案2:直接使用全局断言函数并传入子测试的t
func TestFoo(t *testing.T) { tc := []struct { desc string foo string }{ { desc: "fail abc", foo: "abc", }, { desc: "fail def", foo: "def", }, } for _, tC := range tc { t.Run(tC.desc, func(t *testing.T) { tC := tC assert.Equal(t, tC.foo, "ghi") // 直接传入子测试的t }) } }
修正后的失败输出
修改后,失败结果会清晰显示具体子测试名称:
--- FAIL: TestFoo (0.00s) --- FAIL: TestFoo/fail_abc (0.00s) /test.go:22: Error Trace: /test.go:22 Error: Not equal: expected: "abc" actual : "ghi" Diff: --- Expected +++ Actual @@ -1 +1 @@ -abc +ghi Test: TestFoo/fail_abc --- FAIL: TestFoo/fail_def (0.00s) /test.go:22: Error Trace: /test.go:22 Error: Not equal: expected: "def" actual : "ghi" Diff: --- Expected +++ Actual @@ -1 +1 @@ -def +ghi Test: TestFoo/fail_def FAIL
内容的提问来源于stack exchange,提问作者Jess The Witch
相关产品推荐
相关产品推荐

