13

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.

jacobsa
  • 5,719
  • 1
  • 28
  • 60

2 Answers2

18

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.

gsf
  • 6,612
  • 7
  • 35
  • 64
9

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.

Evan
  • 6,369
  • 1
  • 29
  • 30
  • Thank you! I would prefer a canned `xargs` formula for running in parallel and getting good failure output, but this gets me most of the way there. – jacobsa Feb 16 '15 at 02:15
  • 2
    how does this answer the question? the question was how to run the test without re-compiling! –  Feb 08 '20 at 01:29
  • After compiling the test this way, how do I run it? – Johan Walles Nov 19 '21 at 06:13