go: testing: report non-zero exit status in -json output

I have the following test file sample_test.go:

package main

import (
	"os"
	"testing"
)

func Test(t *testing.T) {
	t.Run("1", func(t *testing.T) {
		t.Run("1.1", func(t *testing.T) {
			os.Exit(2)
                         // os.Exit(0)
		})
	})
}

If I use go test on that file, I get the exit code in the output as expected:

$ go test -json -test.paniconexit0 -test.run Test .      
{"Time":"2023-08-08T16:09:56.448232+02:00","Action":"start","Package":"awesomeProject3"}
{"Time":"2023-08-08T16:09:56.611997+02:00","Action":"run","Package":"awesomeProject3","Test":"Test"}
{"Time":"2023-08-08T16:09:56.612096+02:00","Action":"output","Package":"awesomeProject3","Test":"Test","Output":"=== RUN   Test\n"}
{"Time":"2023-08-08T16:09:56.612131+02:00","Action":"run","Package":"awesomeProject3","Test":"Test/1"}
{"Time":"2023-08-08T16:09:56.612133+02:00","Action":"output","Package":"awesomeProject3","Test":"Test/1","Output":"=== RUN   Test/1\n"}
{"Time":"2023-08-08T16:09:56.612136+02:00","Action":"run","Package":"awesomeProject3","Test":"Test/1/1.1"}
{"Time":"2023-08-08T16:09:56.612138+02:00","Action":"output","Package":"awesomeProject3","Test":"Test/1/1.1","Output":"=== RUN   Test/1/1.1\n"}
{"Time":"2023-08-08T16:09:56.61243+02:00","Action":"output","Package":"awesomeProject3","Test":"Test/1/1.1","Output":"exit status 2\n"}
{"Time":"2023-08-08T16:09:56.612473+02:00","Action":"output","Package":"awesomeProject3","Output":"FAIL\tawesomeProject3\t0.164s\n"}
{"Time":"2023-08-08T16:09:56.612476+02:00","Action":"fail","Package":"awesomeProject3","Elapsed":0.164}

If I use go test2json instead, I will get no exit code in the output at all.

$ go test -c -o awesomeProject3 ./...
$ go tool test2json -t ./awesomeProject3 -test.v -test.run Test       
{"Time":"2023-08-08T16:12:00.158467+02:00","Action":"start"}
{"Time":"2023-08-08T16:12:00.47165+02:00","Action":"run","Test":"Test"}
{"Time":"2023-08-08T16:12:00.471757+02:00","Action":"output","Test":"Test","Output":"=== RUN   Test\n"}
{"Time":"2023-08-08T16:12:00.471767+02:00","Action":"run","Test":"Test/1"}
{"Time":"2023-08-08T16:12:00.47177+02:00","Action":"output","Test":"Test/1","Output":"=== RUN   Test/1\n"}
{"Time":"2023-08-08T16:12:00.471773+02:00","Action":"run","Test":"Test/1/1.1"}
{"Time":"2023-08-08T16:12:00.471775+02:00","Action":"output","Test":"Test/1/1.1","Output":"=== RUN   Test/1/1.1\n"}
{"Time":"2023-08-08T16:12:00.472018+02:00","Action":"fail","Test":"Test/1/1.1","Elapsed":0.314}

Try the same commands with the zero exit code and you will see a detailed panic message in both cases.


  1. Consider panicking on any non-zero exit code as well.
  2. Consider showing the non-zero exit code in the text2json output as well.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 22 (14 by maintainers)

Commits related to this issue

Most upvoted comments

No change in consensus, so accepted. 🎉 This issue now tracks the work of implementing the proposal. — rsc for the proposal review group

When using go tool test2json cmd..., if cmd exits with a non-zero status, then an additional output line would be synthesized:

{"Action":"output","Package":"awesomeProject3","Test":"Test/1/1.1","Output":"exit status 2\n"}

This would match the output printed by go test -json when the test exits with a non-zero status.

This proposal has been added to the active column of the proposals project and will now be reviewed at the weekly proposal review meetings. — rsc for the proposal review group