Reputation: 1036
I want to skip a particular go file under a package, while running the go test and coverage. Please find the below command, I have used to run under my service package :
cd src/service && go test --coverprofile coverageService.out --coverpkg ./.
Upvotes: 2
Views: 4483
Reputation: 1615
Unfortunately there's no simple way to skip tests based solely on the file name, however, here are three simple options which may work:
1. Specify tests using a regular expression
The go test command supports the -run
runtime option which only runs tests satisfying a regular expression:
-run regexp
Run only those tests and examples matching the regular expression. For tests, the regular expression is split by unbracketed slash (/) characters into a sequence of regular expressions, and each part of a test's identifier must match the corresponding element in the sequence, if any. Note that possible parents of matches are run too, so that -run=X/Y matches and runs and reports the result of all tests matching X, even those without sub-tests matching Y, because it must run them to look for those sub-tests.
In order for this to work, you will need to name all of the tests in the file in question in such a way that you can exclude them via a regular expression (for example, make all of them start with TestExcludable...
, such as TestExcludableA
, TestExludableB
, etc). Then run using a slightly hacky negative regular expression, such as -run "Test[^E][^x][^c][^l][^u][^d][^a][^b][^l][^e].*"
2. Don't compile certain tests using build flags
As explained in detail in this stackoverflow post, you can use build flags to prevent a particular file from building as part of the test suite. In a nutshell,
skip_test.go
// +build !skipset1
package name
import testing
// Rest of test file
Then when you invoke go test
us the -tags
parameter and specify the tests you would like to skip, such as -tags skipset1
.
3. Rename the offending file during the build
As specified in the go test docs,
Files whose names begin with "_" (including "_test.go") or "." are ignored.
Simply surround your go test command with a rename so that the file to skip is not actually compiled into the test executable:
mv src/skip_test.go src/_skip_test.go && go test ... || mv src/_skip_test.go src/skip_test.go
(note the use of an ||
and in the second move, to cause it to run even if the go test fails).
Upvotes: 5