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?
-
3`go run -run regexp` will only run tests whos name matches the regular expression. [docs](https://golang.org/cmd/go/#hdr-Description_of_testing_flags) – Ilia Choly Sep 29 '14 at 03:38
-
5Thanks. But should be `go test -run` – lang2 Sep 29 '14 at 05:09
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.
-
14And 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
-
1If 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
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

- 7,635
- 8
- 47
- 79
-
1I'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
-
Given a test:
func Test_myTest() {
//...
}
Run only that test with:
go test -run Test_myTest path/to/pkg/mypackage

- 51,188
- 43
- 183
- 243
-
4This 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
Simple and reliable:
go test -run TestMyFunction ./...
More on ./...
:
https://stackoverflow.com/a/28031651/5726621

- 1,621
- 13
- 16
-
2This was exactly what i was looking for. Thank you - simple and straight to the point – AC007 Feb 23 '22 at 11:39
-
3
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

- 461
- 2
- 5
- 10