How do you print in a Go test using the “testing” package?

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.

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

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

ok      command-line-arguments  0.004s

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.

7 Answers
7

Leave a Comment