Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: output of go test varies by mode #23258

Closed
bhcleek opened this issue Dec 27, 2017 · 5 comments
Closed

cmd/go: output of go test varies by mode #23258

bhcleek opened this issue Dec 27, 2017 · 5 comments

Comments

@bhcleek
Copy link
Contributor

bhcleek commented Dec 27, 2017

What version of Go are you using (go version)?

go version go1.9.2 darwin/amd64
(the same occurs with go version go1.10beta1 darwin/amd64)

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

macOS 10.13.2

GOHOSTARCH="amd64"
GOHOSTOS="darwin"

What did you do?

I executed go test and go test . on a package whose tests will cause output to be written to stderr via the log package:

package main

import (
	"log"
	"testing"
)

func TestHelloWorld(t *testing.T) {
	log.Println("hello, world")
}

What did you expect to see?

I expected the output of local directory mode, (i.e. go test) to be identical to the output of package list mode (i.e. go test . and go test github.com/bhcleek/play/test) with the exception of the elapsed time.

What did you see instead?

In addition to the test summary line, local directory mode output hello, world to stderr and PASS to stdout. Package list mode output only the test summary line.

❯❯❯ go test
2017/12/27 08:28:17 hello, world
PASS
ok      github.com/bhcleek/play/test    0.011s
❯❯❯ go test .
ok      github.com/bhcleek/play/test    0.009s
❯❯❯ go test github.com/bhcleek/play/test
ok      github.com/bhcleek/play/test    0.008s
@mvdan
Copy link
Member

mvdan commented Dec 27, 2017

From go help test:

Go test runs in two different modes: local directory mode when invoked with
no package arguments (for example, 'go test'), and package list mode when
invoked with package arguments (for example 'go test math', 'go test ./...',
and even 'go test .').

In local directory mode, go test compiles and tests the package sources
found in the current directory and then runs the resulting test binary.
In this mode, caching (discussed below) is disabled. After the package test
finishes, go test prints a summary line showing the test status ('ok' or 'FAIL'),
package name, and elapsed time.

In package list 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.

Note the "if a package test passes, go test prints only the final 'ok' summary line". I think that explains what's happening here.

@mvdan mvdan closed this as completed Dec 27, 2017
@bhcleek
Copy link
Contributor Author

bhcleek commented Dec 27, 2017

The documentation seems insufficient in this case; even after reading it, it seems like a stretch that a user would expect local directory mode to include output that package list mode does not.

In addition, the docs here almost seem like there should be a new paragraph starting with the second sentence of the third paragraph you quoted: "If a package test passes...", because otherwise part of that paragraph applies to both local directory mode and package list mode (e.g. the description of -benchmark and -v), while other parts do not (e.g. "go test prints only the final 'ok' summary line").

@mvdan
Copy link
Member

mvdan commented Dec 27, 2017

I was simply pointing out the docs that explain what is going on. If you think the docs could be improved, feel free to reopen and repurpose the issue and send a CL to do the appropriate changes.

@bhcleek
Copy link
Contributor Author

bhcleek commented Dec 27, 2017

Is that preferred over aligning the behavior of local directory mode and package list mode?

@mvdan
Copy link
Member

mvdan commented Dec 27, 2017

If you'd prefer that route, you could write a proposal to try to change the behavior of go test. Bear in mind that you're unlikely to succeed though, as that's a breaking change of some sort. And the current behavior was very likely designed this way on purpose.

@mikioh mikioh changed the title output of go test varies by mode cmd/go: output of go test varies by mode Jan 5, 2018
@golang golang locked and limited conversation to collaborators Jan 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants