Skip to content
Latchkey

Go "go test -run" Matches Nothing - Fix Test Filter Regex in CI

go test -run <regexp> runs only the tests whose names match. A pattern that matches nothing makes Go print no tests to run and exit 0 - so a CI gate can pass while running zero tests - and an invalid regexp aborts the run outright.

What this error means

A -run step prints testing: warning: no tests to run and passes without executing anything, or fails with error parsing regexp for a malformed pattern. Both stem from the -run value, not the tests themselves.

go test output
$ go test -run TestPayed ./...
testing: warning: no tests to run
PASS
# or, a bad regexp:
go test: invalid value "Test(" for flag -run: error parsing regexp: missing closing )

Common causes

The pattern matches no test name

A typo (TestPayed vs TestPaid) or an over-specific anchor means no test name matches, so -run runs nothing and still exits 0.

An invalid regular expression

An unescaped metacharacter (an unbalanced (, a stray [) makes -run fail to parse, aborting the run.

Subtest path separators misunderstood

-run Parent/Child filters subtests by /-joined names; a wrong separator or anchor can silently match no subtest.

How to fix it

List the test names and match them exactly

Confirm the real test names, then write a pattern that matches them.

Terminal
go test -list '.*' ./...       # print all test names
go test -run 'TestPaid$' ./...  # anchored to the exact test

Fail CI when no tests run

Treat "no tests to run" as an error so an empty filter cannot pass silently.

.github/workflows/ci.yml
out=$(go test -run "$PATTERN" ./... 2>&1); echo "$out"
echo "$out" | grep -q 'no tests to run' && { echo 'run matched nothing'; exit 1; } || true

Escape regex metacharacters

  1. Quote the -run value so the shell does not mangle it.
  2. Escape (, ), [, . when matching literal characters in test names.
  3. Use / to target subtests: -run TestX/case_1.

How to prevent it

  • Verify test names with go test -list before relying on -run.
  • Fail CI on "no tests to run" so empty filters do not pass.
  • Quote and escape -run patterns carefully.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →