gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:40:12 26 4
gpt4 key购买 nike

我正在尝试改善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()

关于go - 为什么t.Fail()不接受字符串参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61903715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com