Stop the first test failure with `go test`

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

It takes some time to create and run the rest of the tests, despite the fact that there is already something to work with.

+7
unit-testing go
source share
2 answers

To speed up the build phase, you can run

 go test -i several/packages/... 

before testing to build and install test-dependent packages.

To stop after the first failure you can use something like

 go test several/packages/... | grep FAILED | head -n 1 
-one
source share

Use os.Exit (1) .

 func TestFoo(t *testing.T) { if err := foo(); err != nil { t.Errorf("foo failed: %v", err) // Mark the test as failed. os.Exit(1) // Cancel any further tests. } } 
0
source share

All Articles