为什么 t.Fail() 不接受字符串参数?

Why doesn't t.Fail() accept string arguments?

我正在努力改进我的 Golang 测试。我正在读这个:https://ieftimov.com/post/testing-in-go-failing-tests/

我经常使用 t.Fatal("message"),而我本应该组合使用:

t.Fail()
t.Logf()

那么为什么地球上没有一个调用可以通过测试并记录原因呢?有没有办法将这样的方法添加到 test.Testing 实例?我只想做:

t.FailWithReason("the reason the test failed")

这是否存在,如果不存在,我可以通过某种方式添加它吗?

查看测试包的文档和源代码。

典型用途documentation has an example

func TestAbs(t *testing.T) {
    got := Abs(-1)
    if got != 1 {
        t.Errorf("Abs(-1) = %d; want 1", got)
    }
}

t.Errorf 的文档是:

// Errorf is equivalent to Logf followed by Fail.

这与您所说的非常相似:

t.Fail()
t.Logf()