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?

2

6 Answers

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. 
2

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 Describefunctions.

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 
3

Given a test:

func Test_myTest() { //... } 

Run only that test with:

go test -run Test_myTest path/to/pkg/mypackage 
1

Simple and reliable:

go test -run TestMyFunction ./... 

More on ./... :

2

Say your test suite is structured as following:

type MyTestSuite struct { suite.Suite } func TestMyTestSuite(t *testing.T) { suite.Run(t, new(MyTestSuite)) } func (s *MyTestSuite) TestMethodA() { } 

To run a specific test of test suite in go, you need to use: -testify.m.

 go test -v <package> -run ^TestMyTestSuite$ -testify.m TestMethodA 

More simply, if the method name is unique to a package, you can always run this

go test -v <package> -testify.m TestMethodA 
go test -v <package> -run <TestFunction>