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

pla*_*twp 106 testing go

我正在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.Ttesting.B都有一个.Log.Logf那声音是你在找什么方法..Log.Logf类似于fmt.Printfmt.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

  • 如果有人从 VSCode 运行测试,只需在 settings.json 中添加 `"go.testFlags": ["-v"]` (来源:https://github.com/Microsoft/vscode-go/issues/1377) (18认同)
  • t.Log()将在测试完成之后才会显示,因此如果您正在尝试调试挂起或执行不良的测试,那么您似乎需要使用fmt.请参阅PeterSO的答案,使用go test -v来显示运行测试时fmt.Println的输出. (12认同)

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)

命令去

测试标志的描述

-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(*T)日志

func (c *T) Log(args ...interface{})
Run Code Online (Sandbox Code Playgroud)

Log使用默认格式设置其参数格式,类似于Println,并将文本记录在错误日志中.仅当测试失败或设置了-test.v标志时,才会打印文本.

  • `verbose`就是我想要的. (16认同)
  • anwa在moethod中查看日志输出你正在测试自己 (2认同)

Von*_*onC 14

t.Log()直到测试完成后才会显示,因此如果您尝试调试挂起或性能不佳的测试,似乎您需要使用fmt.

是的:直到 Go 1.13(2019 年 8 月)都包括在内。

然后在golang.org第 24929 期

考虑以下(愚蠢的)自动化测试:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我运行go test -v在所有TestFoo完成之前我都没有日志输出,然后在所有TestBar完成之前没有输出,并且在所有TestBaz完成之前没有更多输出。
如果测试正常,这很好,但是如果存在某种错误,则在某些情况下缓冲日志输出会出现问题:

  • 在本地迭代时,我希望能够进行更改,运行我的测试,立即查看日志中发生的情况以了解发生了什么,如有必要,按 CTRL+C 提前关闭测试,进行另一次更改,重新运行测试,等等。
    如果TestFoo速度很慢(例如,它是一个集成测试),直到测试结束我才得到日志输出。这会显着减慢迭代速度。
  • 如果TestFoo有一个错误导致它挂起并且永远不会完成,我不会得到任何日志输出。在这些情况下,t.Logt.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.Printlnt.Log行是交错的,而不是等待测试完成,证明在go test -v使用时测试输出是流式传输的。

根据戴夫的说法,优势:

对于经常在测试失败时重试的集成样式测试来说,这是一个很好的生活质量改进。
t.Log输出将帮助 Gophers 调试那些测试失败,而不必等到整个测试超时才能接收他们的输出。


Edd*_*dez 9

为了测试有时我会

fmt.Fprintln(os.Stdout, "hello")
Run Code Online (Sandbox Code Playgroud)

此外,您可以打印到:

fmt.Fprintln(os.Stderr, "hello)
Run Code Online (Sandbox Code Playgroud)


Ols*_*nsk 5

警告:当一次测试多个包时,此处的答案并不适用。

@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/...将按预期流式传输输出。