281

The go test command covers *_test.go files in only one dir.

I want to go test the whole project, which means the test should cover all *_test.go files in the dir ./ and every children tree dir under the dir ./.

What's the command to do this?

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
hardPass
  • 19,033
  • 19
  • 40
  • 42

3 Answers3

545

This should run all tests in current directory and all of its subdirectories:

$ go test ./...

This should run all tests for given specific directories:

$ go test ./tests/... ./unit-tests/... ./my-packages/...

This should run all tests with import path prefixed with foo/:

$ go test foo/...

This should run all tests import path prefixed with foo:

$ go test foo...

This should run all tests in your $GOPATH:

$ go test ...
omurbek
  • 742
  • 1
  • 7
  • 23
zzzz
  • 87,403
  • 16
  • 175
  • 139
81

From Go 1.9 onwards, use

go test ./...

In Go 1.6 through 1.8, the ./... matched also the vendor directory. To skip vendored packages, you'd use

go test $(go list ./... | grep -v /vendor/)

Sources: https://github.com/golang/go/issues/11659, https://github.com/golang/go/issues/14417, https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/2366, @nickgrim's comment.

user7610
  • 25,267
  • 15
  • 124
  • 150
  • 1
    As easy as this solution looks, it makes it much more complicated for people on Windows (I'm not but some of my team mates are) to run this. The beauty of Go running native on each platform without the need for a Makefile is great. – Konrad Kleine Sep 14 '16 at 08:47
  • 6
    Worth pointing out that [as of Go 1.9](https://golang.org/doc/go1.9#vendor-dotdotdot) `./...` no longer matches the `./vendor/` directory, so you can run all of your tests and none of the vendor-tests with `go test ./...` – nickgrim Oct 26 '17 at 15:32
16

Folder Structure

ProjectName/folderName1/file_test.go
ProjectName/folderName2/file1_test.go
ProjectName/folderName3/file2_test.go

go test command Command

   ProjectName$ go test -v ./...
    ProjectName$ go test  ./...
    ProjectName$ go test -cover ./...

Coverage Report for the Entire Project

ok      ProjectName/folderName1 10%
ok      ProjectName/folerName2  90%
ok      ProjectName/folerName2  85%