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

1 go go-testing

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

我使用了t.Fatal("message")很多,而我应该使用以下组合:

t.Fail()
t.Logf()
Run Code Online (Sandbox Code Playgroud)

那么到底为什么没有一个调用可以使测试失败并记录原因呢?有没有办法让我将这样的方法添加到 test.Testing 实例中?我只想做:

t.FailWithReason("the reason the test failed")
Run Code Online (Sandbox Code Playgroud)

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

Jak*_*kub 5

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

文档有一个典型使用示例:

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

的文档t.Errorf是:

// Errorf is equivalent to Logf followed by Fail.
Run Code Online (Sandbox Code Playgroud)

这与你所说的非常相似:

t.Fail()
t.Logf()
Run Code Online (Sandbox Code Playgroud)