-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
testing: odd interaction between -json flag and TestMain #31969
Comments
This seems to happen when |
I'm experiencing this same behavior with To add to what is above, it does not matter if you Here is my repro for test2json: package dummy
import (
"os"
"testing"
)
func TestMain(m *testing.M) {
os.Exit(0)
// os.Exit(m.Run())
}
func TestDummy(t *testing.T) {
} Run tests:
Toggling the commented lines in
On a related note, when there are no tests and |
Change https://golang.org/cl/192104 mentions this issue: |
I believe I understand what's going on. The go/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 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 |
When a test has a TestMain that doesn't run any tests (doesn't invoke m.Run), `go test` passes, but `go test -json` reports a "fail" event though the exit code is still 0. This CL fixes test2json to behave similarly to `go test` in such cases - no output from the test is taken as "pass" by default, not as "fail". Fixes golang#31969 Change-Id: I1829d40fc30dc2879e73974fac416f6a34212ccd Reviewed-on: https://go-review.googlesource.com/c/go/+/192104 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
When a test has a TestMain that doesn't run any tests (doesn't invoke m.Run), `go test` passes, but `go test -json` reports a "fail" event though the exit code is still 0. This CL fixes test2json to behave similarly to `go test` in such cases - no output from the test is taken as "pass" by default, not as "fail". Fixes golang#31969 Change-Id: I1829d40fc30dc2879e73974fac416f6a34212ccd Reviewed-on: https://go-review.googlesource.com/c/go/+/192104 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Yup. My suggestion would be to reopen this issue to revert its CL, once the other issue is fixed. |
Change https://golang.org/cl/222243 mentions this issue: |
'go test -json' should report that a test failed if the test binary did not exit normally with status 0. This covers panics, non-zero exits, and abnormal terminations. These tests don't print a final result when run with -test.v (which is used by 'go test -json'). The final result should be "PASS" or "FAIL" on a line by itself. 'go test' prints "FAIL" in this case, but includes error information. test2json was changed in CL 192104 to report that a test passed if it does not report a final status. This caused 'go test -json' to report that a test passed after a panic or non-zero exit. With this change, test2json treats "FAIL" with error information the same as "FAIL" on a line by itself. This is intended to be a minimal fix for backporting, but it will likely be replaced by a complete solution for #29062. Fixes #37555 Updates #29062 Updates #31969 Change-Id: Icb67bcd36bed97e6a8d51f4d14bf71f73c83ac3d Reviewed-on: https://go-review.googlesource.com/c/go/+/222243 Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Change https://golang.org/cl/222658 mentions this issue: |
…r panicking/exiting tests 'go test -json' should report that a test failed if the test binary did not exit normally with status 0. This covers panics, non-zero exits, and abnormal terminations. These tests don't print a final result when run with -test.v (which is used by 'go test -json'). The final result should be "PASS" or "FAIL" on a line by itself. 'go test' prints "FAIL" in this case, but includes error information. test2json was changed in CL 192104 to report that a test passed if it does not report a final status. This caused 'go test -json' to report that a test passed after a panic or non-zero exit. With this change, test2json treats "FAIL" with error information the same as "FAIL" on a line by itself. This is intended to be a minimal fix for backporting, but it will likely be replaced by a complete solution for #29062. Fixes #37671 Updates #37555 Updates #29062 Updates #31969 Change-Id: Icb67bcd36bed97e6a8d51f4d14bf71f73c83ac3d Reviewed-on: https://go-review.googlesource.com/c/go/+/222243 Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> (cherry picked from commit 5ea58c6) Reviewed-on: https://go-review.googlesource.com/c/go/+/222658 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
When running
go test
with this file, the test passes. It also passes withgo test -json
. However, if I tickle the early return inTestMain
: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 runninggo test
here passes, as expected.The text was updated successfully, but these errors were encountered: