使用`go test`命令在第一个测试失败时停止。

huangapple go评论89阅读模式
英文:

Stop on first test failure with `go test`

问题

如何在第一个测试失败后停止go test several/packages/...命令的执行?

尽管已经有一些测试可以工作,但仍然需要花费一些时间来构建和执行其余的测试。

英文:

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.

答案1

得分: 60

Go 1.10在go test中添加了一个新的标志failfast

新的go test -failfast标志在任何测试失败后禁止运行其他测试。请注意,与失败的测试并行运行的测试允许完成。

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

然而,请注意这在跨包时不起作用:https://github.com/golang/go/issues/33038

以下是一个解决方法:

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

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

答案2

得分: -6

为了加快构建阶段的速度,你可以运行以下命令:

go test -i several/packages/...

在运行测试之前,这将构建并安装测试所依赖的包。

如果想在第一个失败后停止测试,可以使用类似以下命令:

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

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

huangapple
  • 本文由 发表于 2015年8月17日 16:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/32046192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定