14

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.

2 Answers 2

25

Use -count N to specify how many times to repeat each test. This will compile the test only once.

Use -failfast to halt the first time it fails.

$ go test ./mypkg -count 1000 -failfast
2
  • 1
    Thanks. Pity that go help test does not advertise it.
    – oxley
    Commented Apr 28, 2021 at 19:24
  • count and other flags are documented in go help testflag. Commented Apr 30, 2021 at 21:30
10

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.

3
  • 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
    Commented Feb 16, 2015 at 2:15
  • 2
    how does this answer the question? the question was how to run the test without re-compiling!
    – user12861522
    Commented Feb 8, 2020 at 1:29
  • After compiling the test this way, how do I run it? Commented Nov 19, 2021 at 6:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.