The "no test files" error occurs because Go cannot find any files matching the _test.go pattern in the current directory or its subdirectories. Ensure your test files are named correctly (e.g., myfunc_test.go), reside in the same package directory, and contain at least one function starting with Test that takes a *testing.T argument.
First, verify your file naming convention. Go's test runner strictly looks for files ending in _test.go. If your file is named myfunc_tests.go or test_myfunc.go, it will be ignored. Additionally, the test functions must be exported (capitalized) and follow the signature func TestXxx(*testing.T).
Here is a minimal, correct example of a test file (main_test.go) that will resolve the error:
package main
import (
"testing"
)
// This function matches the pattern required by 'go test'
func TestHelloWorld(t *testing.T) {
// Example assertion
if "hello" != "hello" {
t.Error("Expected 'hello'")
}
}
If you are running the command from a parent directory containing multiple packages, ensure you specify the correct path or run the command from within the specific package directory. Running go test from the root of a module will recursively test subdirectories, but if you run it from a directory that only contains implementation files (like main.go or utils.go) without any _test.go files, you will see this error.
To quickly verify your setup, list the files in your directory to confirm the pattern exists:
# Check for test files in the current directory
ls -l *_test.go
# Run tests for the current package
go test -v
If you are using a module and the test file is in a subdirectory, you can run tests for the entire module from the root:
# Run tests for all packages in the module
go test ./...
Common pitfalls include placing test files in a different package than the code they test (which requires the _test suffix but a different package declaration) or forgetting to import the testing package. If you are testing external packages, ensure the test file declares package <target_package>_test rather than package main if you are not in the same directory as the source code. However, for most local development, keeping the test file in the same directory with package <name> is the standard approach.