我正在Go中运行一个带有语句的测试来打印某些内容(即用于调试测试),但它不打印任何东西.
func TestPrintSomething(t *testing.T) {
fmt.Println("Say hi")
}
Run Code Online (Sandbox Code Playgroud)
当我对这个文件运行go test时,这是输出:
ok command-line-arguments 0.004s
Run Code Online (Sandbox Code Playgroud)
据我所知,真正让它打印的唯一方法是通过t.Error()打印它,如下所示:
func TestPrintSomethingAgain(t *testing.T) {
t.Error("Say hi")
}
Run Code Online (Sandbox Code Playgroud)
哪个输出:
Say hi
--- FAIL: TestPrintSomethingAgain (0.00 seconds)
foo_test.go:35: Say hi
FAIL
FAIL command-line-arguments 0.003s
gom: exit status 1
Run Code Online (Sandbox Code Playgroud)
我用Google搜索并查看了手册,但没有找到任何内容.
voi*_*gic 119
该结构testing.T
和testing.B
都有一个.Log
和.Logf
那声音是你在找什么方法..Log
和.Logf
类似于fmt.Print
和fmt.Printf
分别.
请在此处查看更多详细信息:http://golang.org/pkg/testing/#pkg-index
fmt.X
打印报表做内部测试工作,但你会发现他们的输出可能不是在你想到哪里去找到它,因此,为什么你应该使用这些日志方法屏幕testing
.
如果像你的情况,你想看到对于未失败的测试日志,你必须提供go test
的-v
标志(v对于冗长).有关测试标志的更多详细信息,请访问:http://golang.org/cmd/go/#hdr-Description_of_testing_flags
pet*_*rSO 115
例如,
package verbose
import (
"fmt"
"testing"
)
func TestPrintSomething(t *testing.T) {
fmt.Println("Say hi")
t.Log("Say bye")
}
Run Code Online (Sandbox Code Playgroud)
go test -v
=== RUN TestPrintSomething
Say hi
--- PASS: TestPrintSomething (0.00 seconds)
v_test.go:10: Say bye
PASS
ok so/v 0.002s
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)-v Verbose output: log all tests as they are run. Also print all text from Log and Logf calls even if the test succeeds.
Run Code Online (Sandbox Code Playgroud)func (c *T) Log(args ...interface{})
Log使用默认格式设置其参数格式,类似于Println,并将文本记录在错误日志中.仅当测试失败或设置了-test.v标志时,才会打印文本.
Von*_*onC 14
t.Log()
直到测试完成后才会显示,因此如果您尝试调试挂起或性能不佳的测试,似乎您需要使用fmt
.
是的:直到 Go 1.13(2019 年 8 月)都包括在内。
考虑以下(愚蠢的)自动化测试:
Run Code Online (Sandbox Code Playgroud)func TestFoo(t *testing.T) { t.Parallel() for i := 0; i < 15; i++ { t.Logf("%d", i) time.Sleep(3 * time.Second) } } func TestBar(t *testing.T) { t.Parallel() for i := 0; i < 15; i++ { t.Logf("%d", i) time.Sleep(2 * time.Second) } } func TestBaz(t *testing.T) { t.Parallel() for i := 0; i < 15; i++ { t.Logf("%d", i) time.Sleep(1 * time.Second) } }
如果我运行
go test -v
,在所有TestFoo
完成之前我都没有日志输出,然后在所有TestBar
完成之前没有输出,并且在所有TestBaz
完成之前没有更多输出。
如果测试正常,这很好,但是如果存在某种错误,则在某些情况下缓冲日志输出会出现问题:
- 在本地迭代时,我希望能够进行更改,运行我的测试,立即查看日志中发生的情况以了解发生了什么,如有必要,按 CTRL+C 提前关闭测试,进行另一次更改,重新运行测试,等等。
如果TestFoo
速度很慢(例如,它是一个集成测试),直到测试结束我才得到日志输出。这会显着减慢迭代速度。- 如果
TestFoo
有一个错误导致它挂起并且永远不会完成,我不会得到任何日志输出。在这些情况下,t.Log
并t.Logf
在所有没有用的。
这使得调试非常困难。- 此外,我不仅没有得到日志输出,而且如果测试挂起太久,要么 Go 测试超时在 10 分钟后终止测试,或者如果我增加该超时,许多 CI 服务器也会终止测试,如果没有一定时间后的日志输出(例如,CircleCI 中的 10 分钟)。
所以现在我的测试被终止了,我没有在日志中告诉我发生了什么。
但是对于(可能)Go 1.14(2020 年第一季度):CL 127120
测试:详细模式下的流日志输出
现在的输出是:
=== RUN TestFoo
=== PAUSE TestFoo
=== RUN TestBar
=== PAUSE TestBar
=== RUN TestGaz
=== PAUSE TestGaz
=== CONT TestFoo
TestFoo: main_test.go:14: hello from foo
=== CONT TestGaz
=== CONT TestBar
TestGaz: main_test.go:38: hello from gaz
TestBar: main_test.go:26: hello from bar
TestFoo: main_test.go:14: hello from foo
TestBar: main_test.go:26: hello from bar
TestGaz: main_test.go:38: hello from gaz
TestFoo: main_test.go:14: hello from foo
TestGaz: main_test.go:38: hello from gaz
TestBar: main_test.go:26: hello from bar
TestFoo: main_test.go:14: hello from foo
TestGaz: main_test.go:38: hello from gaz
TestBar: main_test.go:26: hello from bar
TestGaz: main_test.go:38: hello from gaz
TestFoo: main_test.go:14: hello from foo
TestBar: main_test.go:26: hello from bar
--- PASS: TestFoo (1.00s)
--- PASS: TestGaz (1.00s)
--- PASS: TestBar (1.00s)
PASS
ok dummy/streaming-test 1.022s
Run Code Online (Sandbox Code Playgroud)
它确实在 Go 1.14 中,正如 Dave Cheney 在“go test -v
流输出”中所证明的那样:
在 Go 1.14 中,
go test -v
将在发生时流式传输t.Log
输出,而不是将其囤积到测试运行结束。在 Go 1.14 下,
fmt.Println
和t.Log
行是交错的,而不是等待测试完成,证明在go test -v
使用时测试输出是流式传输的。
根据戴夫的说法,优势:
对于经常在测试失败时重试的集成样式测试来说,这是一个很好的生活质量改进。
流t.Log
输出将帮助 Gophers 调试那些测试失败,而不必等到整个测试超时才能接收他们的输出。
为了测试有时我会
fmt.Fprintln(os.Stdout, "hello")
Run Code Online (Sandbox Code Playgroud)
此外,您可以打印到:
fmt.Fprintln(os.Stderr, "hello)
Run Code Online (Sandbox Code Playgroud)
警告:当一次测试多个包时,此处的答案并不适用。
@VonC 和 @voidlogic 的答案非常棒,但我想提请注意以下线程,以防有人正在运行以下排列go test -v ./...
: https: //github.com/golang/go/issues/46959
问题在于与从多个包运行测试相关的实现细微差别/困难。
例如,运行go test -v -count=1 -run TestOnlyOneInstanceOfThisTestExists ./multiple/packages/exist/below/...
只会在测试完成后打印日志。
但是,运行go test -v -count=1 -run TestOnlyOneInstanceOfThisTestExists ./this/path/points/to/one/package/only/...
将按预期流式传输输出。
归档时间: |
|
查看次数: |
77198 次 |
最近记录: |