gotestsum

repository·main·Indexed 25 days ago

https://github.com/gotestyourself/gotestsum

A test runner for Go that wraps `go test -json` to provide formatted output and summaries for local development and CI environments. It features multiple output formats (such as dots, pkgname, and testdox), JUnit XML and JSON report generation, automatic rerunning of failed tests, and a watch mode to run tests on file save. It also includes a tool to analyze and skip slow tests based on JSON output.

Tokens
4.7K
Snippets
11
Records
37
Agent score
83%

What's inside gotestsum

  1. Install gotestsum

    main

    You can install gotestsum by building from source using go install, or run it directly without installation using go run.

    To install the binary to your $GOPATH/bin:

    go install gotest.tools/gotestsum@latest

    To run it without installing:

    go run gotest.tools/gotestsum@latest

    Alternatively, you can download a pre-compiled binary from the releases page.

  2. Configure test output format

    main

    Use the --format flag or the GOTESTSUM_FORMAT environment variable to control how test names and output are printed. Most formats use color to highlight pass, fail, or skip status.

    Commonly used formats:

    • dots: Prints a character for each test.
    • pkgname (default): Prints a line for each package.
    • testname: Prints a line for each test and package.
    • testdox: Prints a sentence for each test using gotestdox.
    • standard-quiet: The standard go test format.
    • standard-verbose: The standard go test -v format.

    You can also change the icons used by pkgname and testdox formats using the --format-icons flag or the GOTESTSUM_FORMAT_ICONS environment variable. Note that Nerd Fonts icons require a Nerd Font to be installed.

    gotestsum --format dots
  3. Use gotestsum CLI

    main

    The gotestsum CLI is a wrapper around go test that provides enhanced output formats, JUnit XML generation, test rerunning, and file watching.

    Usage Pattern:

    # Basic usage (runs all tests in current directory)
    gotestsum
    
    # Running specific packages with custom go test flags
    gotestsum -- [go test flags] [packages]
    
    # Using a specific output format
    gotestsum --format dots
  4. Re-run failed tests with rerun-fails

    main

    The rerun-fails functionality allows you to automatically re-execute only the tests that failed during a previous run. This is useful for debugging flaky tests.

    Key behaviors:

    • It uses -test.run flags to target specific failed tests or subtests.
    • It can be configured to re-run only the root test cases (ignoring subtests) if rerunFailsRunRootCases is enabled.
    • It supports multiple attempts up to a specified maximum.
    • It can abort the re-run if it detects a panic or a data race (if rerunFailsAbortOnDataRace is enabled).
  5. Use Post-Run Hooks

    main

    You can execute a custom command after the test run completes using postRunHookCmd. The command is executed with the following environment variables available to it:

    Environment VariableDescription
    GOTESTSUM_JSONFILEPath to the JSON event file
    GOTESTSUM_JSONFILE_TIMING_EVENTSPath to the JSON timing events file
    GOTESTSUM_JUNITFILEPath to the JUnit XML file
    GOTESTSUM_ELAPSEDTotal elapsed time in seconds (e.g., 1.234s)
    TESTS_TOTALTotal number of tests
    TESTS_FAILEDNumber of failed tests
    TESTS_SKIPPEDNumber of skipped tests
    TESTS_ERRORSNumber of tests that resulted in errors

    The command's stdout and stderr are piped to the same streams as gotestsum.

  6. Generate a GitHub Actions matrix strategy using the matrix tool

    main

    The matrix tool reads a list of Go packages from stdin and analyzes previous test run timings from log files to output a JSON object. This JSON object can be used as a GitHub Actions matrix strategy to split test packages into parallel partitions, minimizing overall CI runtime by balancing the workload based on historical execution times.

    To use it in a GitHub Actions workflow, pipe the list of packages (e.g., via go list ./...) into the tool and redirect the output to $GITHUB_OUTPUT.

    Usage Example

    echo -n "matrix=" >> $GITHUB_OUTPUT
    go list ./... | matrix --timing-files ./*.log --partitions 4 >> $GITHUB_OUTPUT
  7. Identify and skip slow tests with the `slowest` tool

    main

    The slowest tool reads test output in JSON format (generated by gotestsum --jsonfile or go test -json) and identifies tests that exceed a specified time threshold.

    By default, it prints a list of slow tests to stdout, sorted from slowest to fastest. If the --skip-stmt flag is provided, the tool modifies the Go source code in your working directory to inject a skip statement into the slow test functions, allowing you to bypass them in future runs.

  8. Execute a compiled test binary

    main

    To run a test binary created with go test -c, use --raw-command combined with go tool test2json to convert the binary's output into the JSON format gotestsum expects.

    Note: You must include the -test.v flag for test2json to work correctly.

    gotestsum --raw-command -- go tool test2json -t -p pkgname ./binary.test -test.v
  9. Configure rerun-fails report file

    main

    If rerunFailsMaxAttempts is greater than 0 and a report file path is provided via rerunFailsReportFile, gotestsum will generate a report summarizing the results of the re-runs. The report format is:

    <package>.<test_name>: <total_runs> runs, <failure_count> failures

    Example report line: github.com/user/repo/pkg.TestExample: 3 runs, 1 failures

  10. Configure JSON file output

    main

    You can instruct gotestsum to write test events to JSON files. This is useful for machine-readable logs and post-processing.

    • jsonFile: The path to the file where all test events will be written (one JSON object per line).
    • jsonFileTimingEvents: The path to a file where only terminal test events (events that mark the end of a test) are written. This is useful for analyzing test durations.