What is go-junit-report?
go-junit-report is a tool that converts go test
output to JUnit compatible XML, suitable for use with CI applications like Jenkins.
Enabling XML Output
go-junit-report converts the output of go test
to JUnit compatible XML. First install it with
1go install github.com/jstemmer/go-junit-report/v2@latest
then run it from inside your go project like this:
1go test -v 2>&1 ./... | go-junit-report -set-exit-code > report.xml
Test Suite naming
go-junit-report produces JUnit XML with the name and classname fields set by the function name and classnames being tested. For example, this function in the module example/hello
1func TestHelloName(t *testing.T) {2 name := "Gladys"3 want := regexp.MustCompile(`\b`+name+`\b`)4 msg, err := Hello("Gladysx")5 if !want.MatchString(msg) || err != nil {6 t.Fatalf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want)7 }8}
will produce XML output that looks like:
1<testsuites tests="1" failures="1">2 <testsuite name="example/hello" tests="1" failures="1" errors="0" id="0" hostname="Joshs-MacBook-Air.local" time="0.386" timestamp="2024-08-06T11:51:57-07:00">3 <testcase name="TestHelloName" classname="example/hello" time="0.000">4 <failure message="Failed"><![CDATA[ hello_test.go:17: Hello("Gladys") = "Great to see you, Gladysx!", <nil>, want match for `\bGladys\b`, nil]]></failure>5 </testcase>6 </testsuite>7</testsuites>8
Handling Retries
Go does not have built in retry support. If you need to retry a lot of tests that is a strong indicator that you may have flaky tests. These are tests that sometimes pass and sometimes fail, slowing down your development process. You can get early access to Trunk’s new Flaky Tests tool to manage these types of tests better.