You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
testing: odd interaction between -json flag and TestMain #31969
When running go test with this file, the test passes. It also passes with go test -json. However, if I tickle the early return in TestMain:
$ XYZ=2 go test -json
{"Time":"2019-05-10T09:33:09.534255206-07:00","Action":"output","Package":"_testing/testmain","Output":"ok \t_testing/testmain\t0.019s\n"}
{"Time":"2019-05-10T09:33:09.534473515-07:00","Action":"fail","Package":"_testing/testmain","Elapsed":0.019}
Note the fail status in the last line. The exit code of this go test -json run is 0, even though it (wrongly) reported failure. Regularly running go test here passes, as expected.
$ go test -c -o test-exec
$ go tool test2json -t ./test-exec
{"Time":"2019-08-27T10:10:53.931641+09:00","Action":"fail","Elapsed":0.005}
Toggling the commented lines in TestMain() results in:
$ go test -c -o test-exec
$ go tool test2json -t ./test-exec
{"Time":"2019-08-27T10:11:51.007087+09:00","Action":"output","Output":"PASS\n"}
{"Time":"2019-08-27T10:11:51.007417+09:00","Action":"pass","Elapsed":0.006}
On a related note, when there are no tests and m.Run() is called, the test2json shows "passed" even though the docs state "skip - the test was skipped or the package contained no tests"
I believe I understand what's going on. The test commands uses a converter from test2json to filter its stdout. test2json looks for PASS lines to mark a "pass" and FAIL lines to mark a "fail", but if no such lines are encountered in the stdout of the running test, it currently defaults to "fail" -- see
I sent https://golang.org/cl/192104 to change this to "pass", and all tests pass except one where the current behavior is explicitly assumed.
Trying to think of any bad side effects of this... In general, the behavior of test2json is hereby aligned to go test, which will take a "no tests run" as a passing event.
Note: this change makes the behavior similar to running in a directory with no tests, where test2json currently reports a "pass".
I'm not positive that this is the right fix. In #29062, the fix is to make go test fail if the test binary didn't print PASS, so I think we should apply the same fix to test2json.
Change https://golang.org/cl/222658 mentions this issue: [release-branch.go1.14] cmd/go: make go test -json report failures for panicking/exiting tests
Activity
andybons commentedon May 13, 2019
@mpvl @josharian
hanleym commentedon May 14, 2019
This seems to happen when
TestMainreturns withoutm.Run()being called; regardless ofos.Exit().ryanbrainard commentedon Aug 27, 2019
I'm experiencing this same behavior with
test2json. I'm assuming they share the same code path, so that makes sense.To add to what is above, it does not matter if you
returnfromTestMain()oros.Exit(0)fromTestMain(), they both exhibit this behavior.Here is my repro for test2json:
Run tests:
Toggling the commented lines in
TestMain()results in:On a related note, when there are no tests and
m.Run()is called, thetest2jsonshows "passed" even though the docs state "skip - the test was skipped or the package contained no tests"gopherbot commentedon Aug 28, 2019
Change https://golang.org/cl/192104 mentions this issue:
test2json: default to "pass" when the test doesn't report failureseliben commentedon Aug 28, 2019
I believe I understand what's going on. The
testcommands uses a converter from test2json to filter its stdout. test2json looks for PASS lines to mark a "pass" and FAIL lines to mark a "fail", but if no such lines are encountered in the stdout of the running test, it currently defaults to "fail" -- seego/src/cmd/internal/test2json/test2json.go
Line 287 in 7254cfc
I sent https://golang.org/cl/192104 to change this to "pass", and all tests pass except one where the current behavior is explicitly assumed.
Trying to think of any bad side effects of this... In general, the behavior of test2json is hereby aligned to
go test, which will take a "no tests run" as a passing event.Note: this change makes the behavior similar to running in a directory with no tests, where test2json currently reports a "pass".
mvdan commentedon Aug 31, 2019
I'm not positive that this is the right fix. In #29062, the fix is to make
go testfail if the test binary didn't printPASS, so I think we should apply the same fix to test2json.eliben commentedon Aug 31, 2019
@mvdan the main complaint in this issue was that
-jsonhas different semantics from no-json, and the CL aligns them.If the original semantics of no-json are reversed, then the CL should be reverted.
I'll comment on #29062
test2json: default to "pass" when the test doesn't report failures
test2json: default to "pass" when the test doesn't report failures
mvdan commentedon Sep 9, 2019
Yup. My suggestion would be to reopen this issue to revert its CL, once the other issue is fixed.
gopherbot commentedon Mar 5, 2020
Change https://golang.org/cl/222243 mentions this issue:
cmd/go: make go test -json report failures for panicking/exiting testscmd/go: make go test -json report failures for panicking/exiting tests
gopherbot commentedon Mar 9, 2020
Change https://golang.org/cl/222658 mentions this issue:
[release-branch.go1.14] cmd/go: make go test -json report failures for panicking/exiting tests[release-branch.go1.14] cmd/go: make go test -json report failures fo…