Go tip of the day : running tests for all subpackages recursively

Submitted by Frederic Marand on

If you program in Go, you've probably written a lot of packages, and probably split packages in subpackages. Maybe even more than idiomatic Go would really advise... And you may have been grumbling just like I did at the fact that the go test command requires a list of packages, and does not recursively dive into all the subpackages, like PHPunit would, and does not seem to have a working recursion flag.

The trick is that with Go tools recursion is defined directly in the way one lists packages. All it takes to run all tests below the current package directory is just:

go test ./...

This is actually just an application of the rules explained by go help packages about import paths: An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns. As a special case, x/... matches x as well as x's subdirectories. For example, net/... expands to net and packages in its subdirectories. (emphasis added).

Best of all, since this is a common "packages" option, it also works for other go commands, like go vet or go fmt.

And what about go test ... ? That one will run tests on all packages below the GOPATH.