104

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?

bjb568
  • 11,089
  • 11
  • 50
  • 71
lang2
  • 11,433
  • 18
  • 83
  • 133

6 Answers6

112

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.
c6754
  • 870
  • 1
  • 9
  • 15
Simon Fox
  • 5,995
  • 1
  • 18
  • 22
  • 14
    And as a `regexp`, if you want to run a precise test (and no other tests that may share that name as a prefix) - use regexp's first and last anchors e.g. `go test '-run=^TestMyTest$'` – colm.anseo Aug 12 '19 at 16:00
  • 1
    If you are using subtests, keep in mind that you are passing a slash-separated list of regular expressions, so something like `go test -r "TestFoo/^bar$/baz` is valid. – freb Dec 31 '20 at 18:46
  • This answer didn't work for me. I got the error `...build constraints exclude all Go files in...` Instead, I needed to do `go test -run=TestMyTest ./...` – Ross Smith II Aug 07 '23 at 23:24
48

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
Bunyk
  • 7,635
  • 8
  • 47
  • 79
  • 1
    I'm trying to do this, but inside 'var _ = g.Describe(...), but it it isn't working, and still runs the other tests. Do you have any idea why? I'm a bit new to Go. – Joshua Oliphant Jun 07 '18 at 22:32
  • @JoshuaOliphant For me it happens when there are more than one package with tests, and it runs tests in all packages. Then, even in one package tests are focused - it runs other packages. But to figure out your problem I need more details. – Bunyk Jun 26 '18 at 14:23
  • Just F it! Super easy to remember, nice one ginkgo – Christian Stewart Mar 24 '22 at 06:03
35

Given a test:

func Test_myTest() {
    //...
}

Run only that test with:

go test -run Test_myTest path/to/pkg/mypackage
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
  • 4
    This is quite helpful, I didn't know what should be expressed with RegExp but this sample just made me understand in 1s – artgb Jun 29 '20 at 03:11
25

Simple and reliable:

go test -run TestMyFunction ./...

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

neox5
  • 1,621
  • 13
  • 16
15

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
Master Oogway
  • 461
  • 2
  • 5
  • 10
12
go test -v <package> -run <TestFunction>
Sergey Onishchenko
  • 6,943
  • 4
  • 44
  • 51