Just run single test instead of the whole suite?
I have a test suite for a Go package that implements a dozen tests. Sometimes, one of the tests in the suite fails and I'd like to re-run that test individually to save time in debugging process. Is this possible or do I have to write a separate file for this every time?
Use the go test -run
flag to run a specific test. The flag is documented in
the testing flags section of the go tool documentation:
-run regexp
Run only those tests and examples matching the regular
expression.
Given a test:
func Test_myTest() {
//...
}
Run only that test with:
go test -run Test_myTest path/to/pkg/mypackage
In case someone that is using Ginkgo BDD framework for Go will have the same problem, this could be achieved in that framework by marking test spec as focused (see docs), by prepending F
before It
, Context
or Describe
functions.
So, if you have spec like:
It("should be idempotent", func() {
You rewrite it as:
FIt("should be idempotent", func() {
And it will run exactly that one spec:
[Fail] testing Migrate setCurrentDbVersion [It] should be idempotent
...
Ran 1 of 5 Specs in 0.003 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 4 Skipped