The testing package captures all output and will not print it unless a test fails or verbose (-v
) is turned on. Is there a way to output text after the suite is finished without needing verbose turned on?
For example:
func TestMain(m *testing.M) {
status := m.Run(m)
fmt.Println("important line to output")
os.Exit(status)
}
Will not print the line.
Edit: I have found out that fmt.Println
will work if you run the tests from inside the package (go test
), but not if one or more packages are specified (go test ./...
) unless the -v
option is enabled.
The testing package captures all output and will not print it unless a test fails or verbose (-v) is turned on
What makes you say that stdout is captured? If I save this code into main_test.go
and run go test
:
package main
import (
"fmt"
"os"
"testing"
)
func TestJoe(t *testing.T) {
fmt.Println("from joe")
}
func TestMain(m *testing.M) {
fmt.Println("before run")
s := m.Run()
fmt.Println("after run")
os.Exit(s)
}
I get:
$ go test
before run
from joe
PASS
after run
About output with ./...
, here's an excerpt from go help test
:
The second, called package list mode, occurs when go test is invoked with explicit package arguments (for example 'go test math', 'go test ./...', and even 'go test .'). In this mode, go test compiles and tests each of the packages listed on the command line. If a package test passes, go test prints only the final 'ok' summary line. If a package test fails, go test prints the full test output. If invoked with the -bench or -v flag, go test prints the full output even for passing package tests, in order to display the requested benchmark results or verbose logging.