"Can I run just one test file?" — yes, and here's how to do it in Go
Have you ever sat in front of the terminal, typed go test ./..., and watched in horror as your entire Go project's codebase started running hundreds of tests? But you only needed to check one small test file you just finished writing. Sound familiar? If so, you've come to the right place.
In this article, we'll break down a popular Stack Overflow question (which, by the way, has thousands of views): "How to run test cases in a specified file?". We'll adapt the answers to the realities of a Go developer and show not only basic commands but also life hacks that will save you hours of work. Let's go!
Why isn't plain go test always suitable?
In large projects, tests are divided into unit, integration, and end-to-end. If you run go test ./..., Go recursively traverses all subpackages and executes all found tests. This can take minutes. And if you have tests that access a database or external APIs, they might fail for reasons beyond your control. Therefore, the ability to run test cases in a specified file is not just a convenience, but a professional developer skill.
Basic command: go test with the file name
The most straightforward way is to specify the specific test file. But there's a nuance: Go requires you to specify both the source file and the test file together. Why? Because the compiler needs to compile all the code in the package.
Suppose you have a project structure:
myproject/├── main.go└── math_utils.go└── math_utils_test.goTo run tests only from math_utils_test.go, execute:
go test -v math_utils.go math_utils_test.goThe -v (verbose) flag will output detailed information about each test case. Without it, you'll only see the result: PASS or FAIL.
Real code example
Let's create a simple file math_utils.go:
package main
func Sum(a, b int) int { return a + b}
func Multiply(a, b int) int { return a * b}And a test file math_utils_test.go:
package main
import "testing"
func TestSum(t *testing.T) { result := Sum(2, 3) if result != 5 { t.Errorf("Sum(2, 3) = %d; want 5", result) }}
func TestMultiply(t *testing.T) { result := Multiply(3, 4) if result != 12 { t.Errorf("Multiply(3, 4) = %d; want 12", result) }}Now let's run it:
$ go test -v math_utils.go math_utils_test.go=== RUN TestSum--- PASS: TestSum (0.00s)=== RUN TestMultiply--- PASS: TestMultiply (0.00s)PASSok command-line-arguments 0.002sNotice the output: ok command-line-arguments. This means Go compiled a temporary package from the specified files. This approach works but has limitations.
The problem with multiple files in one package
If your package consists of several files (e.g., math_utils.go and strings_utils.go), and a test in math_utils_test.go calls a function from strings_utils.go, the command above will fail with a compilation error. Go will complain that the function is not defined.
The solution is to list all dependent files. But this is inconvenient. It's better to use a different approach.
Advanced method: filtering tests by name (-run)
The -run flag allows you to run only those test cases whose names match a regular expression. This is not tied to a file, but if you name your tests meaningfully, you can easily filter the ones you need.
Example: run all tests starting with TestSum:
go test -v -run T