Skip to content

testing: report non-zero exit status in -json output #61839

@goto1134

Description

@goto1134

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Accepted

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions