通过“ go test”在第一次测试失败时停止

通过“ 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.

我如何让去测试几个/包/... code>在第一个之后停止 测试失败? p>

尽管已经有一些需要配合的工作,但仍需要一些时间来构建和执行其余测试。 p> div>

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

To speed-up the build phase you can run

go test -i several/packages/...

before the tests to build and install packages that are dependencies of the test.

To stop after the first failure you can use something like

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

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.
    }
}