What is gotestsum?
gotestsum is as tool that converts go test
output to JUnit compatible XML, suitable for use with CI applications like Jenkins.
Enabling XML Output
Since go test
does not support XML output directly, you can use gotestsum to convert the native output to JUnit XML. First install it with
1go install gotest.tools/gotestsum@latest
Then run your tests with
1gotestsum --junitfile gotestsum_test.xml
Test Suite naming
gotestsum 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}
produces XML that looks like this:
1<testsuites tests="1" failures="1" errors="0" time="0.555454">2 <testsuite tests="1" failures="1" time="0.363000" name="example/hello" timestamp="2024-08-06T14:25:47-07:00">3 <properties>4 <property name="go.version" value="go1.22.5 darwin/arm64"></property>5 </properties>6 <testcase classname="example/hello" name="TestHelloName" time="0.000000">7 <failure message="Failed" type="">=== RUN TestHelloName
8 hello_test.go:17: Hello("Gladys") = "9 Hail, Gladysx! Well met!", <nil>, want10 match for `\bGladys\b`, nil
--- FAIL: TestHelloName (0.00s)11 </failure>12 </testcase>13 </testsuite>14</testsuites>15
Handling Retries
gotestsum supports retrying using --rerun-fails
command line argument. See the documentation for more details.
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.