Is there a way to display success messages in the "testing" package when writing unit test cases in Go?

huangapple go评论138阅读模式
英文:

Is there a way to display success messages in the "testing" package when writing unit test cases in Go?

问题

我正在使用测试包来测试我的代码。目前,当错误不为'nil'时,我只记录错误日志,例如:

if err != nil {
  t.Errorf("Failed to initialise Client %s", err)
}

我还想在错误为nil时使用相同的 t *testing.T 实例打印成功消息。有没有办法做到这一点?

英文:

I am using testing package to test my code. Currently I am only logging errors when errors are not 'nil' like:

if err != nil {
  t.Errorf("Failed to initialise Client %s", err)
}

I also want to print success messages when error is nil using the same t *testing.T instance. Is there a way to do this?

答案1

得分: 2

你可以使用Log或Logf方法:

t.Log("它的作用类似于Println")
t.Logf("它的作用类似于Printf")

更多详细信息请查看官方文档

英文:

You can use Log or Logf methods:

t.Log("It works like Println")
t.Logf("It works like Printf")

Check official documents for more details.

huangapple
  • 本文由 发表于 2022年9月5日 19:44:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/73608860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定