Gotestsum
'go test' runner with output optimized for humans, JUnit XML for CI integration, and a summary of the test results.
Install / Use
/learn @gotestyourself/GotestsumREADME
gotestsum
gotestsum runs tests using go test -json, prints formatted test output, and a summary of the test run.
It is designed to work well for both local development, and for automation like CI.
gotestsum is used by some of the most popular Go projects.
Install
Download a binary from releases, or build from
source with go install gotest.tools/gotestsum@latest. To run without installing use
go run gotest.tools/gotestsum@latest.
Documentation
Core features
- Change the test output format, from compact to verbose with color highlighting.
- Print a summary of the test run after running all the tests.
- Use any
go testflag, run a script with--raw-command, or run a compiled test binary.
CI and Automation
--junitfile- write a JUnit XML file for integration with CI systems.--jsonfile- write all the test2json input received bygotestsumto a file. The file can be used as input togotestsum tool slowest, or as a way to store the full verbose output of tests when less verbose output is printed to stdout using a compact--format.--rerun-fails- run failed (possibly flaky) tests again to avoid re-running the entire suite. Re-running individual tests can save significant time when working with flaky test suites.
Local Development
--watch- every time a.gofile is saved run the tests for the package that changed.--post-run-command- run a command after the tests, can be used for desktop notification of the test run.gotestsum tool slowest- find the slowest tests, or automatically update the source code of the slowest tests to add a conditionalt.Skipstatements. This statement allows you to skip the slowest tests usinggotestsum -- -short ./....
Output Format
The --format flag or GOTESTSUM_FORMAT environment variable set the format that
is used to print the test names, and possibly test output, as the tests run. Most
outputs use color to highlight pass, fail, or skip.
The --format-icons flag changes the icons used by pkgname and testdox formats.
You can set the GOTESTSUM_FORMAT_ICONS environment variable, instead of the flag.
The nerdfonts icons requires a font from Nerd Fonts.
Commonly used formats (see --help for a full list):
dots- print a character for each test.pkgname(default) - print a line for each package.testname- print a line for each test and package.testdox- print a sentence for each test using gotestdox.standard-quiet- the standardgo testformat.standard-verbose- the standardgo test -vformat.
Have an idea for a new format? Please share it on github!
Demo
A demonstration of three --format options.
<br /><sup>Source</sup>
Summary
Following the formatted output is a summary of the test run. The summary includes:
-
The test output, and elapsed time, for any test that fails or is skipped.
-
The build errors for any package that fails to build.
-
A
DONEline with a count of tests run, tests skipped, tests failed, package build errors, and the elapsed time including time to build.DONE 101 tests[, 3 skipped][, 2 failures][, 1 error] in 0.103s
To hide parts of the summary use --hide-summary section.
Example: hide skipped tests in the summary
gotestsum --hide-summary=skipped
Example: hide everything except the DONE line
gotestsum --hide-summary=skipped,failed,errors,output
# or
gotestsum --hide-summary=all
Example: hide test output in the summary, only print names of failed and skipped tests and errors
gotestsum --hide-summary=output
JUnit XML output
When the --junitfile flag or GOTESTSUM_JUNITFILE environment variable are set
to a file path, gotestsum will write a test report, in JUnit XML format, to the file.
This file can be used to integrate with CI systems.
gotestsum --junitfile unit-tests.xml
If the package names in the testsuite.name or testcase.classname fields do not
work with your CI system these values can be customized using the
--junitfile-testsuite-name, or --junitfile-testcase-classname flags. These flags
accept the following values:
short- the base name of the package (the single term specified by the package statement).relative- a package path relative to the root of the repositoryfull- the full package path (default)
Note: If Go is not installed, or the go binary is not in PATH, the GOVERSION
environment variable can be set to remove the "failed to lookup go version for junit xml"
warning.
JSON file output
When the --jsonfile flag or GOTESTSUM_JSONFILE environment variable are set
to a file path, gotestsum will write a line-delimited JSON file with all the
test2json
output that was written by go test -json. This file can be used to compare test
runs, or find flaky tests.
gotestsum --jsonfile test-output.log
Post Run Command
The --post-run-command flag may be used to execute a command after the
test run has completed. The binary will be run with the following environment
variables set:
GOTESTSUM_ELAPSED # test run time in seconds (ex: 2.45s)
GOTESTSUM_FORMAT # gotestsum format (ex: pkgname)
GOTESTSUM_JSONFILE # path to the jsonfile, empty if no file path was given
GOTESTSUM_JUNITFILE # path to the junit.xml file, empty if no file path was given
TESTS_ERRORS # number of errors
TESTS_FAILED # number of failed tests
TESTS_SKIPPED # number of skipped tests
TESTS_TOTAL # number of tests run
To get more details about the test run, such as failure messages or the full list of failed
tests, run gotestsum with either a --jsonfile or --junitfile and parse the
file from the post-run-command. The
gotestsum/testjson
package may be used to parse the JSON file output.
Example: desktop notifications
First install the example notification command with go get gotest.tools/gotestsum/contrib/notify.
The command will be downloaded to $GOPATH/bin as notify. Note that this
example notify command only works on Linux with notify-send and on macOS with
terminal-notifer installed.
On Linux, you need to have some "test-pass" and "test-fail" icons installed in your icon theme.
Some sample icons can be found in contrib/notify, and can be installed with make install.
On Windows, you can install notify-send.exe but it does not support custom icons so will have to use the basic "info" and "error".
gotestsum --post-run-command notify
Example: command with flags
Positional arguments or command line flags can be passed to the --post-run-command by
quoting the whole command.
gotestsum --post-run-command "notify me --date"
Example: printing slowest tests
The post-run command can be combined with other gotestsum commands and tools to provide
a more detailed summary. This example uses gotestsum tool slowest to print the
slowest 10 tests after the summary.
gotestsum \
--jsonfile tmp.json.log \
--post-run-command "bash -c '
echo; echo Slowest tests;
gotestsum tool slowest --num 10 --jsonfile tmp.json.log'"
Re-running failed tests
When the --rerun-fails flag is set, gotestsum will re-run any failed tests.
The tests will be re-run until each passes once, or the number of attempts
exceeds the maximum attempts. Maximum attempts defaults to 2, and can be changed
with --rerun-fails=n.
To avoid re-running tests when there are real failures, the re-run will be
skipped when there are too many test failures. By default this value is 10, and
can be changed with --rerun-fails-max-failures=n.
You may use the --rerun-fails-abort-on-data-race flag to abort the re-run if
a data race is detected.
Note that using --rerun-fails may require the use of other flags, depending on
how you specify args to go test:
-
when used with
--raw-commandthe re-run will pass additional arguments to the command. The first arg is a-test.runflag with a regex that matches the test to re-run, and second is the name of a go package. These additional args can be passed togo test, or a test binary. -
when used with any
go testargs (anything after--on the command line), the list of packages to test must be specified as a space separated list using the--packagesarg.Example
gotestsum --rerun-fails --packages="./..." -- -count=2 -
if any of the
go testargs should be passed to the test binary, instead ofgo testitself, the-argsflag must be used to separate the two groups of arguments.-argsis a special flag that is understood bygo testto indicate that any following args should be passed directly to the test binary.Example
gotestsum --rerun-fails --packages="./..." -- -count=2 -args -update-golden
Custom go test command
By default gotestsum runs tests using the command go test -json ./.... You
can change th
