我使用一条语句在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我在谷歌上搜索并查看了手册,但没有找到任何东西。
发布于 2019-06-12 22:41:45
为了测试,有时我会这样做
fmt.Fprintln(os.Stdout, "hello")此外,您还可以打印到:
fmt.Fprintln(os.Stderr, "hello)发布于 2020-04-29 09:28:24
t.Log和t.Logf确实会在您的测试中打印出来,但经常会被遗漏,因为它与您的测试打印在同一行上。我所做的是以一种让他们脱颖而出的方式记录他们,即
t.Run("FindIntercomUserAndReturnID should find an intercom user", func(t *testing.T) {
id, err := ic.FindIntercomUserAndReturnID("test3@test.com")
assert.Nil(t, err)
assert.NotNil(t, id)
t.Logf("\n\nid: %v\n\n", *id)
})其将其打印到终端,
=== RUN TestIntercom
=== RUN TestIntercom/FindIntercomUserAndReturnID_should_find_an_intercom_user
TestIntercom/FindIntercomUserAndReturnID_should_find_an_intercom_user: intercom_test.go:34:
id: 5ea8caed05a4862c0d712008
--- PASS: TestIntercom (1.45s)
--- PASS: TestIntercom/FindIntercomUserAndReturnID_should_find_an_intercom_user (1.45s)
PASS
ok github.com/RuNpiXelruN/third-party-delete-service 1.470s发布于 2021-02-24 10:30:12
如果您使用testing.M和相关的设置/拆卸;-v在这里也是有效的。
package g
import (
"os"
"fmt"
"testing"
)
func TestSomething(t *testing.T) {
t.Skip("later")
}
func setup() {
fmt.Println("setting up")
}
func teardown() {
fmt.Println("tearing down")
}
func TestMain(m *testing.M) {
setup()
result := m.Run()
teardown()
os.Exit(result)
}$ go test -v g_test.go
setting up
=== RUN TestSomething
g_test.go:10: later
--- SKIP: TestSomething (0.00s)
PASS
tearing down
ok command-line-arguments 0.002shttps://stackoverflow.com/questions/23205419
复制相似问题