How to run all Golang benchmark tests including subfolders

Issue

I have a Go application with a number of unit and benchmark tests both in the root and in a subfolder called "message".

I execute the following command to run all unit tests from the root including the ones in the messages and any other subfolder:

go test ./...

I want to achieve the same for the benchmark tests, i.e. run them all. The following works for the ones in the root directory:

go test -bench .

The benchmark tests in the /messages folder are ignored which is expected. So I run the following from the root:

go test -bench ./...

That’s not recognised at all, Go seems to execute the unit tests that are located in the root dir. I even tried to specify the message folder in the command as follows:

go test -bench ./message

…but it also failed. Currently if I want to run the benchmark tests in the message folder I have to cd into that folder and execute

go test -bench .

like above.

So what’s the correct way then? How can I tell Go to find the benchmark tests both in the root and the subfolders? How does the regexp arg work in the case of the -bench flag? Apparently it’s different from the regexp for the unit test runner.

Solution

You should use ./... to bench all the files from the current working directory and all of its subdirectories. If you wish to get a more verbose output you can use the -v flag. Also it’s good to list the memory allocation by using -benchmem.

go test -v ./... -bench=. -run=xxx -benchmem

Answered By – Endre Simo

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.