Question
The go test command only runs tests in *_test.go files for a single directory.
I want to run tests for the entire Go project, including all *_test.go files in the current directory (./) and in every nested subdirectory beneath it.
What command should I use to do this?
Short Answer
By the end of this page, you will understand how go test works with Go packages, how to run tests across an entire project, and why commands like go test ./... are commonly used in real Go codebases.
Concept
In Go, go test works at the package level, not by manually searching for individual *_test.go files.
That means when you run:
go test
Go tests the package in the current directory only.
If your project contains multiple packages in subdirectories, those tests are not automatically included unless you tell Go to test more than one package.
The common solution is:
go test ./...
The pattern ./... means:
- start from the current directory
- include the current package
- include all subpackages recursively
This matters because most real Go projects are split into multiple packages such as:
./api./db./internal/auth./utils
Each of those directories may contain its own *_test.go files. Using go test ./... ensures all package tests are run in one command.
This package-based model is an important part of how Go organizes code, builds software, and runs tests.
Mental Model
Think of a Go project as a building with many rooms.
- Each directory/package is one room.
go testchecks only the room you are currently standing in.go test ./...walks through the current room and then opens every room inside the building beneath it.
So the command is not saying “find test files manually.” It is saying “test every package starting here.”
Syntax and Examples
The main syntax is:
go test ./...
Example project structure
myapp/
├── go.mod
├── main.go
├── main_test.go
├── api/
│ ├── handlers.go
│ └── handlers_test.go
├── db/
│ ├── store.go
│ └── store_test.go
└── internal/
└── auth/
├── auth.go
└── auth_test.go
Commands
Test only the current directory:
go test
Test all packages from the current directory downward:
go test ./...
Test with verbose output:
go test -v ./...
Test without using cached results:
go test -count=1 ./...
What happens
go testruns tests in the current package only.go test ./...runs tests in the current package and all nested packages.- Go automatically detects
*_test.gofiles inside each package.
Step by Step Execution
Consider this structure:
project/
├── math/
│ ├── add.go
│ └── add_test.go
└── stringutil/
├── trim.go
└── trim_test.go
If you are inside project/ and run:
go test
Step by step
- Go looks at the current directory:
project/ - It checks whether
project/itself is a Go package with testable files. - It does not automatically enter
math/orstringutil/. - Tests in subdirectories are skipped.
Now run:
go test ./...
Step by step
./...tells Go to start at the current directory.- Go finds all packages under that path.
- It includes
./math. - It includes
./stringutil. - For each package, Go compiles the package and its tests.
- It runs all matching test functions such as
TestAddand .
Real World Use Cases
Running tests across all packages is useful in many everyday situations:
- Before committing code: make sure no package was broken by your changes.
- In CI pipelines: GitHub Actions, GitLab CI, and other tools commonly run
go test ./.... - During refactoring: if you rename functions or move shared logic, you want all package tests to run.
- In monorepo-style Go services: multiple internal packages need to be tested together.
- Before release: verify the full project still behaves correctly.
Example CI command:
go test ./...
Example with more detail:
go test -v ./...
Example for a fresh run:
go test -count=1 ./...
Real Codebase Usage
In real Go codebases, developers rarely run tests file by file. Instead, they use package patterns and flags.
Common patterns include:
-
Test everything
go test ./... -
Verbose output for debugging CI failures
go test -v ./... -
Disable cache when investigating flaky behavior
go test -count=1 ./... -
Run only tests matching a name
go test ./... -run TestLogin -
Collect coverage across packages
go test ./... -cover
Developers also organize tests by package boundaries. This means each folder usually owns its own tests, and one top-level command runs them all.
This style fits well with:
- modular package design
- automated validation in CI
- safe refactoring
- quick feedback loops during development
Common Mistakes
1. Expecting go test to recurse automatically
Broken assumption:
go test
Many beginners expect this to test every folder. It does not. It only tests the current package.
Use:
go test ./...
2. Thinking Go searches only by filename
Go does not simply scan the filesystem for *_test.go files and run them independently. Test files belong to a package and are run as part of that package.
3. Running from the wrong directory
If you run:
go test ./...
from a subdirectory, Go starts there, not from the project root.
To test the full project, run the command from the module or project root when appropriate.
4. Confusing packages with folders that are not Go packages
A directory is only tested if it is a valid Go package. Random folders without Go files are ignored.
5. Forgetting about cached test results
Sometimes you change environment-dependent behavior and wonder why tests seem unchanged.
Use:
go test -count=1 ./...
Comparisons
| Command | What it tests | Recursive | Common use |
|---|---|---|---|
go test | Current package only | No | Quick test in one directory |
go test . | Current package only | No | Explicit current package |
go test ./... | Current package and all subpackages | Yes | Test the whole project |
go test ./api | One specific package | No | Test a selected package |
go test ./... -run TestName | All packages, filtered by test name | Yes |
Cheat Sheet
# Test current package only
go test
# Test current package explicitly
go test .
# Test current package and all subpackages
go test ./...
# Verbose output
go test -v ./...
# Disable test cache
go test -count=1 ./...
# Run only matching test names
go test ./... -run TestName
# Run with coverage
go test ./... -cover
Rules to remember
- Go tests packages, not standalone test files.
*_test.gofiles are included automatically within each package.go testis not recursive.go test ./...is recursive from the current directory.- Run from the correct project or module root when you want the whole project.
Most important answer
go test ./...
FAQ
How do I run all tests in a Go project?
Use:
go test ./...
This runs tests in the current directory and all subpackages.
Does go test automatically include subdirectories?
No. By itself, go test only runs tests for the current package.
What does ./... mean in Go?
It is a package pattern meaning the current directory and everything below it recursively.
Do I need to specify *_test.go files manually?
No. Go automatically finds and runs test files inside each package.
Why are some folders not being tested?
A folder must contain a valid Go package to be tested. Non-Go folders are ignored.
How do I rerun all tests without cached results?
Use:
go test -count=1 ./...
Can I run all tests with verbose output?
Yes:
go test -v ./...
Should I run this command from the project root?
Usually yes, if you want to test the entire module or project tree.
Mini Project
Description
Create a small multi-package Go project and practice running tests across the entire project. This demonstrates that Go testing works at the package level and that go test ./... is the standard way to run all package tests together.
Goal
Build a small Go module with multiple packages and verify that one command runs every test.
Requirements
- Create a Go module with at least two subpackages.
- Add one normal
.gofile and one*_test.gofile in each package. - Write at least one passing test function per package.
- Run tests for only one package first, then run all tests recursively.
- Confirm that
go test ./...reports results for each package.
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.