Go语言 表驱动测试在使用tested时总是显示父测试失败

pdtvr36n  于 2023-04-18  发布在  Go
关注(0)|答案(1)|浏览(157)

当将testify与表驱动测试一起使用时,如下所示:

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)。如果表中有很多测试,这可能会使测试完全无用。

yhxst69z

yhxst69z1#

当初始化testify简写时,它必须使用子测试中的*testing.T。如果你和我一样习惯将a := assert.New(t)放在每个测试的顶部,你最终会从父测试中传递*testing.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
            a := assert.New(t) // Now using *testing.T from within the sub-test
            a.Equal(tC.foo, "ghi")
        })
    }
}

获取预期的清晰输出(即按子测试和测试名称(包括描述)细分的故障):

--- FAIL: TestFoo (0.00s)
    --- FAIL: TestFoo/fail_abc (0.00s)
        boop_test.go:27: 
                Error Trace:    /test.go:27
                Error:          Not equal: 
                                expected: "abc"
                                actual  : "ghi"
                            
                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -abc
                                +ghi
                Test:           TestFoo/fail_abc
    --- FAIL: TestFoo/fail_def (0.00s)
        boop_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_def
FAIL

相关问题