Search code examples
unit-testinggo

Stop on first test failure with `go test`


How do I have go test several/packages/... stop after the first test failure?

It takes some time to build and execute the rest of the tests, despite already having something to work with.


Solution

  • Go 1.10 added a new flag failfast to go test:

    The new go test -failfast flag disables running additional tests after any test fails. Note that tests running in parallel with the failing test are allowed to complete.

    https://golang.org/doc/go1.10

    However, note this does not work across packages: https://github.com/golang/go/issues/33038

    Here's a workaround:

    for s in $(go list ./...); do if ! go test -failfast -v -p 1 $s; then break; fi; done