英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论