Just run single test instead of the whole suite?

Go

Go Problem Overview


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?

Go Solutions


Solution 1 - Go

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.

Solution 2 - Go

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

Solution 3 - Go

Given a test:

func Test_myTest() {
    //...
}

Run only that test with:

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

Solution 4 - Go

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

Solution 5 - Go

Simple and reliable:

go test -run TestMyFunction ./...

More on ./... : https://stackoverflow.com/a/28031651/5726621

Solution 6 - Go

go test -v <package> -run <TestFunction>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionlang2View Question on Stackoverflow
Solution 1 - GoSimon FoxView Answer on Stackoverflow
Solution 2 - GoBunykView Answer on Stackoverflow
Solution 3 - GoCory KleinView Answer on Stackoverflow
Solution 4 - GoMaster OogwayView Answer on Stackoverflow
Solution 5 - Goneox5View Answer on Stackoverflow
Solution 6 - GoSergey OnishchenkoView Answer on Stackoverflow