Skip to content
Muhammad Septian
All projects

gotestimpact

Runs only the Go tests affected by your changes, by tracing which test functions can reach the changed code through a static reference graph.

Year
2026
Built with
Go, Static analysis, CI
Links

On a large codebase go test ./... can take a long time, even though a pull request usually touches a handful of functions. gotestimpact looks at what changed on your branch and in your working tree, works out which test functions can reach the changed code, and runs just those with go test -run.

$ gotestimpact -- -race
gotestimpact: changes since origin/main (merge-base a1b2c3d): 37 changed file(s)
gotestimpact: type-checking 15 package(s) to select test functions
gotestimpact: 8 changed package(s): 86 test function(s) in 4 package(s), all tests in 2 package(s), 98 of 104 test package(s) skipped

How it works

  1. Changed files come from git diff against the merge base, plus untracked files.
  2. Files map to packages through go list -test, which also covers _test.go files, //go:embed files, and files excluded by build tags.
  3. Packages map to tests: a test package is selected when it changed or depends on a changed package, directly or transitively.
  4. Tests are narrowed to functions. The packages in between are type-checked and a reference graph is built between top-level declarations. Changed lines are mapped to the declarations that contain them, and a test is selected when it reaches one of them.
  5. Run go test -run '^(TestA|TestB|...)$' on those packages, and the full suite on packages that need it.

Erring on the side of running more

Static analysis can’t see every call, so the graph is deliberately generous:

  • A call through an interface depends on every method with the same name and arity.
  • Methods that satisfy interfaces outside the module (String, ServeHTTP, MarshalJSON, …) select everything that creates a value of that type.
  • Passing a function as a value counts as using it.
  • Creating a struct depends on its layout, which covers reflection such as json.Marshal.
  • Changes reaching init, side-effect variables, or blank imports run every test that links the package.
  • Comment-only and formatting edits select nothing.

A change to go.mod, go.sum, or go.work runs the whole suite, and -run-all-on adds your own patterns, such as migrations or .proto files. -explain shows why each test was selected and why each file was ignored.

In CI

Fetch the base branch history, install the tool, and run it against the PR’s base:

- run: go install github.com/mseptiaan/gotestimpact@latest
- run: gotestimpact -base origin/${{ github.base_ref }} -explain -- -race

Running the full suite on the main branch, on merge or nightly, still catches anything the analysis can’t see.