319

My package test cases are scattered across multiple files, if I run go test <package_name> it runs all test cases in the package.

It is unnecessary to run all of them though. Is there a way to specify a file for go test to run, so that it only runs test cases defined in the file?

8 Answers8

517

There are two ways. The easy one is to use the -run flag and provide a pattern matching names of the tests you want to run.

Example:

$ go test packageName -run NameOfTest

See the docs for more info.

Note that the -run flag may also run other tests if they contain the string NameOfTest, as the -run flag matches a regexp.

So to ensure that only a test named exactly 'NameOfTest' is run, one has to use the regexp ^NameOfTest$:

$ go test -run "^NameOfTest$" 

The other way is to name the specific file, containing the tests you want to run:

$ go test foo_test.go

But there's a catch. This works well if:

  • foo.go is in package foo.
  • foo_test.go is in package foo_test and imports 'foo'.

If foo_test.go and foo.go are the same package (a common case) then you must name all other files required to build foo_test. In this example it would be:

$ go test foo_test.go foo.go

I'd recommend to use the -run pattern. Or, where/when possible, always run all package tests.

nullptr
  • 3,701
  • 2
  • 16
  • 40
zzzz
  • 87,403
  • 16
  • 175
  • 139
  • 1
    would you mind to elaborate on the first (easy) way with `-run` please? I cannot find any manual reference on that one. –  Jun 05 '13 at 09:46
  • 1
    You can also find info about it with `go help testflag` – Jeffrey Martinez Jan 31 '15 at 16:30
  • 1
    When I use the command `go test utils.go utils_test.go` the output is `ok command-line-arguments 0.002s`. It doesn't run the test. The reason I want to use this test mode is because the package contains many files and they don't compile yet except utils. So I would like to finalize utils before workinqg on other files. How can I do that ? – chmike Dec 03 '16 at 12:12
138

@zzzz's answer is mostly complete, but just to save others from having to dig through the referenced documentation you can run a single test in a package as follows:

go test packageName -run TestName

Note that you want to pass in the name of the test, not the file name where the test exists.

The -run flag actually accepts a regex so you could limit the test run to a class of tests. From the docs:

-run regexp
    Run only those tests and examples matching the regular
    expression.
TomDotTom
  • 6,238
  • 3
  • 41
  • 39
29

When running a single test I usually do:

go test -run TestSomethingReallyCool ./folder1/folder2/ -v -count 1

-count 1 also ensures that the test is ran every time instead of being cached. Useful when you are testing against race conditions and have a test that fails only sometimes. In Go versions not using modules the same could be achieved by setting GOCACHE=off but this interacts poorly with Go modules.

Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
  • 1
    You can also add the `-race` flag to different go commands like `go test`, to help detect where race conditions happen: https://golang.org/doc/articles/race_detector.html – xuiqzy Jan 11 '21 at 18:05
7
go test -v ./<package_name> -run Test

Prevents caching of test results.

go test -count=1 ./<package_name> -run Test
Dele
  • 730
  • 6
  • 10
  • 1
    Quick note, if your cmd line is at the same level as the *_test.go file, you need to prefix the package name with `../` instead of `./` – NateH06 Mar 16 '21 at 00:15
7

Visual Studio Code shows a link at the top of a Go test file which lets you run all the tests in just that file.

enter image description here

In the "Output" window, you can see that it automatically generates a regex which contains all of the test names in the current file:

Running tool: C:\Go\bin\go.exe test -timeout 30s -run ^(TestFoo|TestBar|TestBaz)$ rootpackage\mypackage

Note: the very first time you open a Go file in VS Code it automatically offers to install some Go extensions for you. I assume the above requires that you have previously accepted the offer to install.

Tim Macinta
  • 119
  • 1
  • 6
5
alias testcases="sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /|/g'"

go test -v -run $(cat coordinator_test.go | testcases)
user2865442
  • 59
  • 1
  • 1
0
go test -v -timeout 30s <path_to_package> -run ^(TestFuncRegEx)
  • The TestFunc must be inside a go test file in that package
  • We can provide a regular expression to match a set of test cases or just the exact test case function to run a single test case. For instance -run TestCaseFunc
0

I was facing some issue to run a single specific test from my package this is how I solved

go test -v -race ./parsers/xml2csv/stld/ -timeout 30s -run '^Test_stldItemParser_readNodes$'

If I run without single quote it wasn't working I use oh-my-zsh in Ubuntu-20.04

Here

-v             for verbosity
-race          to detect race condition
-timeout       for the timeout of the test

This are optional can be omitted but good to add.

ARIF MAHMUD RANA
  • 5,026
  • 3
  • 31
  • 58