Video in progress

Staff 🧪 Testing

In a monorepo, a full 40-minute test suite runs on every PR. How do you design running only the tests relevant to the change?

Based on the build graph, I find the "affected" modules and test only them and the modules that depend on them (at the Gradle/Bazel level), then as a safety measure I run the full suite on main or in the merge queue. Test impact analysis is a trade-off between speed and the risk of skipping something.

📖 Answer

There are two levels of selection.

At the module level. The build system knows which module changed and who depends on it. If billing-core changes, the tests of billing-core, payout-service and admin-api run, and catalog-ui isn't touched. Bazel makes this natural; in Gradle it's the build cache + the configuration graph; in Maven it's -pl ... -amd. This is safe and deterministic: the dependency graph is explicit.

At the test level (test impact analysis). Collect which classes each test uses from earlier coverage runs, and pick the tests that touch the changed classes. It cuts more, but is riskier:

  • reflection, Spring configuration, application.yml and SQL migration files don't show up in the coverage map;
  • changes to static resources and dependency versions look like "touched nothing".

So you need a safety net:

  • if configuration, build files or migrations change, the full suite;
  • a merge queue: a PR passes the full suite before entering main (or several PRs together);
  • the full suite after every commit on main, so a skipped bug is caught within hours and the culprit commit is clear.

One more layer is a remote build cache: the test result of an unchanged module is taken from the cache and doesn't run at all. This often gives more speed than selection.

What I measure: PR feedback time and the "escape rate", the number of tests not selected in the PR but failing on main. If the escape rate starts to grow, the selection rules are too aggressive.