Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 599
Matt Joiner Avatar asked Aug 17 '15 08:08

Matt Joiner


People also ask

Does go test run go vet?

As part of building a test binary, go test runs go vet on the package and its test source files to identify significant problems. If go vet finds any problems, go test reports those and does not run the test binary.

How do you stop a jest test?

By default, Jest runs all your tests and shows their result in the console. Yet, Jest comes with a bail option allowing you to stop running tests after n failed tests. You may configure the bail option to a boolean value or a number. Using a boolean for bail will either stop or not stop after the first failed test.

What is t test go?

The testing package provides support for automated testing of the Golang code. To run any test function use “go test” command, which automates the execution of any function of the form TestXxx(*testing. T), where Xxx must not start with any lowercase letter. Test Function Syntax : func TestXxx(*testing.T)

How do you test a go function?

At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.


1 Answers

Go 1.10 add 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 
like image 114
gao Avatar answered Oct 07 '22 04:10

gao