Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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.

like image 818
jacobsa Avatar asked Feb 13 '15 04:02

jacobsa


People also ask

How do I run go test without cache?

To disable test caching, use any test flag or argument other than the cacheable flags. The idiomatic way to disable test caching explicitly is to use -count=1.

How do I run multiple test cases in Golang?

go test : to run all _test.go files in the package. go test -v : will display the result of all test cases with verbose logging. go test -run TestFunctionName/Inputvalue= : to run the test case for specific input. Here I run the `TestSumNumbersInList` for only the 5th index of input.

Are go tests run sequentially?

By default, execution of test code using the testing package will be done sequentially. However, note that it is only the tests within a given package that run sequentially. If tests from multiple packages are specified, the tests will be run in parallel at the package level.

How do I run a specific test in Golang?

To launch the test under the cursor, invoke the test runner via Ctrl + Shift + F10 on Windows/Linux or ^ + ⇧ + R on macOS.


2 Answers

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.

like image 186
gsf Avatar answered Sep 21 '22 21:09

gsf


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.

like image 42
Evan Avatar answered Sep 22 '22 21:09

Evan