The user with no hat
The user with no hat

Reputation: 10846

Running tests and skipping some packages

Is it possible to skip directories from testing. For example given the structure below is it possible to test mypackage, mypackage/other and mypackage/net but not mypackage/scripts? I mean without to write a go test command for each ( e.g. go test; go test net; go test other)

mypackage
mypackage/net
mypackage/other
mypackage/scripts

Upvotes: 33

Views: 27626

Answers (1)

Dave C
Dave C

Reputation: 7878

Go test takes a list of packages to test on the command line (see go help packages) so you can test an arbitrary set of packages with a single invocation like so:

go test import/path/to/mypackage import/path/to/mypackage/other import/path/to/mypackage/net

Or, depending on your shell:

go test import/path/to/mypackage{,/other,/net}


You might be able to use interesting invocations of go list as the arguments (again, depending on your shell):

go test `go list`

Your comment says you want to skip one sub-directory so (depending on your shell) perhaps this:

go test `go list ./... | grep -v directoriesToSkip`

as with anything, if you do that a lot you could make a shell alias/whatever for it.


If the reason you want to skip tests is, for example, that you have long/expensive tests that you often want to skip, than the tests themselves could/should check testing.Short() and call t.Skip() as appropriate.

Then you could run either:

go test -short import/path/to/mypackage/...

or from within the mypackage directory:

go test -short ./...

You can use things other testing.Short() to trigger skipping as well.

Upvotes: 56

Related Questions