Golang:如何在不重新编译的情况下重复运行”go test”命令?

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

Golang: How to run "go test" repeatedly without recompiling?

问题

有没有办法让我轻松地运行 Go 测试多次,在第一次失败时停止?我当然可以像这样做:

for i in {1..1000}; do go test ./mypkg && break; done

但是这样每次都会重新编译,与测试本身相比非常慢。我想我可以通过巧妙地使用 -exec 标志和 xargs 来实现这一点,但我不擅长一行命令。

如果在一千次中的一两次失败时能以某种合理的详细输出并行运行它,那就更好了。

英文:

Is there any way for me to easily run a Go test many times, halting the first
time it fails? I can of course do something like this:

for i in {1..1000}; do go test ./mypkg && done

but that causes a recompile every time, which is very slow compared to the test
itself. I imagine I could do this with some clever application of the -exec
flag and xargs, but I am not good at one-liners.

Bonus points for running it many times in parallel with some semblance of sane
verbose output if it fails one or two out of a thousand times.

答案1

得分: 18

这可能是一个新功能-你可以使用-count N来指定每个测试重复运行的次数。可能值得一提的是,它将使用单个编译来运行这些测试。

我必须感谢Florin Pățan在我们最近的Github讨论中注意到了这一点。

英文:

It is probably new feature - but you can use -count N to specify how many times to repeat each test. Probably worth mentioning it will run them with a single compilation.

I have to thank Florin Pățan for noticing that in a Github discussion we recently had.

答案2

得分: 9

你可以在go test命令中加上-c标志,根据帮助文档的说明:

> 编译测试二进制文件为pkg.test,但不运行它。(其中pkg是包导入路径的最后一个元素)

这样,你至少可以避免每次都重新编译。

英文:

You can pass the -c flag to go test which will, per the help:

> Compile the test binary to pkg.test but do not run it. (Where pkg is the last element of the package's import path.)

So you can at least avoid recompiling every single time.

huangapple
  • 本文由 发表于 2015年2月13日 12:48:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/28492694.html
匿名

发表评论

匿名网友

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

确定