Question
My Go package has test cases spread across multiple _test.go files. When I run go test <package_name>, it executes every test in that package.
I do not always need to run all of them. Is there a way to tell go test to run only the test cases defined in one specific file, instead of running the entire package?
Short Answer
By the end of this page, you will understand how go test selects tests, why it works at the package level rather than the file level, and how to run only the tests you want using flags like -run. You will also learn practical patterns for organizing and targeting tests in real Go projects.
Concept
In Go, go test is designed around packages, not individual test files. When you run a test command for a package, Go builds a temporary test binary from:
- the package source files
- all matching
_test.gofiles in that package - any test helpers and test functions found there
That means Go does not normally treat one _test.go file as an isolated unit to execute on its own. Instead, it compiles the whole package test suite together.
If you want to run only some tests, the usual approach is to filter by:
- test name using
-run - benchmark name using
-bench - subtest name using
-runwith slash-separated matching - package by pointing
go testat a specific package path
This matters in real programming because test suites grow quickly. Running every test every time can be slow, especially when:
- a package has many unit tests
- some tests hit databases, files, or network mocks
- you are debugging one feature and want quick feedback
So the key idea is:
- Go does not provide a standard “run this exact test file only” workflow
- Go does provide a clean way to run selected tests by name
If your tests in one file share a naming pattern, you can target them easily with -run.
Mental Model
Think of a Go package like a classroom, and each _test.go file like a worksheet stored in that classroom.
When go test runs, it does not pick up just one worksheet and ignore the rest. Instead, it gathers all worksheets in the classroom, builds one test session, and then decides which exercises to perform.
So:
- package = the room Go enters
- test files = papers collected from that room
- test functions = the individual exercises
-run= a filter that says “only do exercises whose names match this pattern”
This is why the usual solution is not “choose a file,” but “choose the test names inside the package.”
Syntax and Examples
The main syntax for running selected Go tests is:
go test ./path/to/package -run PATTERN
PATTERN is a regular expression matched against test names.
Example: run one test
package mathutil
import "testing"
func TestAdd(t *testing.T) {
// test logic
}
func TestSubtract(t *testing.T) {
// test logic
}
Run only TestAdd:
go test ./mathutil -run ^TestAdd$
The ^ and $ anchors mean:
^= start of string$= end of string
So only the exact test name TestAdd matches.
Example: run multiple tests with a shared prefix
Step by Step Execution
Consider this package:
package greeting
import "testing"
func TestHelloEnglish(t *testing.T) {}
func TestHelloSpanish(t *testing.T) {}
func TestGoodbyeEnglish(t *testing.T) {}
Command:
go test ./greeting -run ^TestHello
Here is what happens step by step:
- Go locates the
greetingpackage. - It collects the package source files.
- It collects all
_test.gofiles in that package. - It builds a test binary containing all test functions in that package.
- It checks the
-runpattern:^TestHello. - It compares the pattern to each test name.
- These tests match:
TestHelloEnglishTestHelloSpanish
- This test does not match:
Real World Use Cases
Here are common situations where selective test execution is useful:
Debugging one feature
You are fixing user login logic and only want to run:
go test ./auth -run ^TestLogin
Working with slow integration tests
A package contains both fast unit tests and slower database tests. Naming slow tests clearly helps you run just what you need.
go test ./store -run ^TestUserRepo
Running subtests for one scenario
If a test uses subtests, you can target one case precisely.
func TestParser(t *testing.T) {
t.Run("valid_json", func(t *testing.T) {})
t.Run("invalid_json", func(t *testing.T) {})
}
Run one subtest:
go test ./parser -run 'TestParser/invalid_json'
Faster inner development loop
During development, running one focused test repeatedly is much faster than running the entire suite every time.
Real Codebase Usage
In real Go codebases, developers usually solve this package-vs-file limitation with naming, structure, and test patterns.
Common pattern: grouped test names
Tests related to one feature often share a prefix:
func TestInvoiceCreate(t *testing.T) {}
func TestInvoiceDelete(t *testing.T) {}
func TestInvoiceValidate(t *testing.T) {}
Then they can be run together:
go test ./billing -run ^TestInvoice
Common pattern: subtests
A single top-level test can group related scenarios:
func TestCheckout(t *testing.T) {
t.Run("empty_cart", func(t *testing.T) {})
t.Run("invalid_coupon", func(t *testing.T) {})
t.Run("success", func(t *testing.T) {})
}
This makes it easy to run one scenario without splitting logic across many test functions.
Common Mistakes
Mistake 1: expecting go test to run one _test.go file in a package
Many beginners assume this should work as a standard package-testing command:
go test user_test.go
Problem:
- Go testing normally works on packages, not on individual test files.
- Running tests this way is not the usual solution for package-based testing.
- It can behave differently from a real package test run.
Use this instead:
go test ./mypkg -run ^TestUser
Mistake 2: using a loose -run pattern
Broken example:
go test ./mypkg -run Test
This may match many tests, not just one.
Safer exact match:
go test ./mypkg -run ^TestCreateUser$
Mistake 3: forgetting that -run uses regular expressions
This command does not mean “contains these literal characters only” in a simple string sense:
Comparisons
| Approach | What it selects | Best for | Notes |
|---|---|---|---|
go test ./mypkg | All tests in one package | Full package verification | Standard package-level run |
go test ./... | All tests in many packages | Whole project runs | Useful in CI |
go test ./mypkg -run ^TestName$ | Specific test(s) by name | Focused debugging | Most common selective method |
go test ./mypkg -run ^TestGroup | Tests with a shared prefix | Feature-based grouping | Good if tests are named consistently |
go test ./mypkg -short |
Cheat Sheet
# Run all tests in one package
go test ./mypkg
# Run all tests in all packages
go test ./...
# Run one exact test
go test ./mypkg -run ^TestAdd$
# Run tests by prefix
go test ./mypkg -run ^TestUser
# Run a subtest
go test ./mypkg -run 'TestParser/invalid_json'
# Run fast tests only (if tests use testing.Short)
go test -short ./...
Key rules
- Go test execution is package-based.
_test.gofiles in the same package are compiled together.- Use
-runto execute only matching tests. -runuses a regular expression.- Use
^and$for exact matches.
Good naming pattern
func TestUserCreate(t *testing.T) {}
func TestUserDelete(t *testing.T) {}
{}
FAQ
Can go test run only one test file?
Not as a normal package-level testing feature. Go tests are designed to run at the package level, and the usual way to limit execution is with -run by test name.
How do I run one test in Go?
Use -run with the exact test name:
go test ./mypkg -run ^TestMyCase$
How do I run tests from one file if they are grouped there?
Go does not directly select by file, but if the tests in that file share a naming pattern, you can run them together with -run.
Does -run support subtests?
Yes. You can match subtests using slash-separated names, such as:
go test ./mypkg -run 'TestParser/invalid_json'
Why does Go use packages instead of files for testing?
Because Go treats the package as the main unit of compilation and testing. All relevant test files in the package are built together.
What is the best way to organize tests for selective runs?
Use consistent test names, group related cases with prefixes, and use subtests for focused scenarios.
Can I make slow tests optional?
Yes. Use testing.Short() inside tests and run fast-only checks with:
Mini Project
Description
Create a small Go test suite for a calculator package and practice running only selected tests using go test -run. This demonstrates the real Go workflow: tests live across files in one package, but you target them by name rather than by file.
Goal
Build a package with multiple tests and learn to run one test or one group of related tests without executing the whole suite.
Requirements
- Create a Go package named
calculator. - Add simple functions such as
Add,Subtract, andMultiply. - Write tests for those functions across at least two
_test.gofiles. - Name the tests so related ones share a clear prefix.
- Run only the add-related tests using
go test -run.
Keep learning
Related questions
Blank Identifier Imports in Go: What `_` Means in an Import Statement
Learn what `_` means in a Go import, why blank identifier imports run package init code, and when to use them safely.
Check if a Value Exists in a Slice in Go
Learn how to check whether a value exists in a slice in Go, and why Go has no Python-style `in` operator for arrays or slices.
Concatenating Slices in Go with append
Learn how to concatenate two slices in Go using append and the ... operator, with examples, pitfalls, and practical usage.