Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running tests and skipping some packages

Tags:

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 
like image 911
The user with no hat Avatar asked Mar 15 '15 17:03

The user with no hat


People also ask

Should go tests be in the same package?

High level tests (integration, acceptance, etc...) should probably be placed in a separate package to ensure that you are using the package via the exported API. If you have a large package with a lot of internals that need to be put under test then use the same package for your tests.

How do you run a test in Golang?

At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.


1 Answers

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.

like image 159
Dave C Avatar answered Sep 21 '22 05:09

Dave C