我正在努力改进我的Golang测试。我正在读这个:https://ieftimov.com/post/testing-in-go-failing-tests/
我经常使用t.Fatal("message")
,而相反,我应该使用以下组合:
t.Fail()
t.Logf()
那么为什么地球上没有一个电话然后可以失败测试并记录原因呢?有没有办法让我将这种方法添加到测试中。测试实例? 我只想做:
t.FailWithReason("the reason the test failed")
这是否存在,如果不存在,我可以以某种方式添加它吗?
查看测试包的文档和源代码。
该文档有一个典型用法的示例:
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()