gpt4 book ai didi

testing - 如何使用 "testing"包在 Go 测试中打印?

转载 作者:IT老高 更新时间:2023-10-28 12:57:18 34 4
gpt4 key购买 nike

我正在 Go 中运行一个测试,其中包含打印某些内容的语句(即用于调试测试),但它没有打印任何内容。

func TestPrintSomething(t *testing.T) {
fmt.Println("Say hi")
}

当我对该文件运行 go test 时,输出如下:

ok      command-line-arguments  0.004s

据我所知,真正让它打印的唯一方法是通过 t.Error() 打印它,如下所示:

func TestPrintSomethingAgain(t *testing.T) {
t.Error("Say hi")
}

哪个输出这个:

Say hi
--- FAIL: TestPrintSomethingAgain (0.00 seconds)
foo_test.go:35: Say hi
FAIL
FAIL command-line-arguments 0.003s
gom: exit status 1

我用谷歌搜索并查看了手册,但没有找到任何东西。

最佳答案

结构 testing.Ttesting.B两者都有 .Log.Logf听起来是您正在寻找的方法。 .Log.Logf类似于 fmt.Printfmt.Printf分别。

在此处查看更多详细信息:http://golang.org/pkg/testing/#pkg-index

fmt.X 打印语句 do 在测试中工作,但您会发现它们的输出可能不在您希望找到的屏幕上,因此,为什么您应该使用 testing 中的日志记录方法。

如果您想查看没有失败的测试的日志,则必须提供 go test -v 标志(v 表示冗长)。更多关于测试标志的细节可以在这里找到:https://golang.org/cmd/go/#hdr-Testing_flags

关于testing - 如何使用 "testing"包在 Go 测试中打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23205419/

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