如何使用“测试”功能在Go测试中进行打印?包? [英] How do you print in a Go test using the "testing" package?

查看:170
本文介绍了如何使用“测试”功能在Go测试中进行打印?包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm running a test in Go with a statement to print something (i.e. for debugging of tests) but it's not printing anything.

我在Go中运行一个测试,用语句打印某些内容(即用于调试测试),但它不打印任何内容。 c $ c> func TestPrintSomething(t * testing.T){
fmt.Println(Say hi)
}

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

当我在这个文件上运行go test时,输出如下:

When I run go test on this file, this is the output:

ok      command-line-arguments  0.004s

要真正打印它的唯一方法,就我而言知道,就是通过t.Error()打印它,如下所示:

The only way to really get it to print, as far as I know, is to print it via t.Error(), like so:

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



Which outputs this:

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

我已谷歌搜索并查看了手册,但没有找到任何内容。

I've Googled and looked through the manual but didn't find anything.

推荐答案

结构 testing.T testing.B 都有 .Log .Logf 方法,这听起来就是你正在寻找的。 .Log .Logf 类似于 fmt.Print fmt.Printf

The structs testing.T and testing.B both have a .Log and .Logf method that sound to be what you are looking for. .Log and .Logf are similar to fmt.Print and fmt.Printf respectively.

在这里查看更多详情:http://golang.org/pkg/testing/#pkg-index

See more details here: http://golang.org/pkg/testing/#pkg-index

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

fmt.X print statements do work inside tests, but you will find their output is probably not on screen where you expect to find it and, hence, why you should use the logging methods in testing.

如果你想要查看没有失败的测试日志,您可以提供 go test -v 标志(v为详细信息) 。有关测试标志的更多详细信息,请访问: http://golang.org/cmd/go/#hdr-Description_of_testing_flags

If, as in your case, you want to see the logs for tests that are not failing, you have provide go test the -v flag (v for verbosity). More details on testing flags can be found here: http://golang.org/cmd/go/#hdr-Description_of_testing_flags

这篇关于如何使用“测试”功能在Go测试中进行打印?包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆