r/devops • u/breadMSA • Jun 21 '26
Discussion Running only the tests a git diff affects in CI - the CI-shareable part is the hard bit
Disclaimer (Rule 4): I'm the author of the tool I mention at the end, so treat this as self-promotion. I'm posting because the CI/CD side is what I actually want to discuss, not to sell anything.
Context: on bigger pipelines the full test suite runs on every PR even when the change is one line. The usual answers are sharding/parallelism (faster, but you still run everything) or test impact analysis, run only the tests a change can actually reach. TIA is well understood on the build-graph side (Bazel and Pants track this natively), but for a plain pytest suite in CI the options are thinner.
The part that's specifically a CI/CD problem, not a local-dev one: most TIA tools store their "which test touches what" map on the developer's machine. That does nothing for your pipeline. For CI you need the map to be shareable, committed or cached as an artifact, keyed by git ref, and able to survive a shallow clone (CI checkouts are usually --depth 1).
Three things I learned trying to make this work in a pipeline:
- The map has to resolve a diff without
git show, because shallow clones don't have the history. Baking the function tables into the artifact was what fixed that. - Whether it's worth it depends entirely on how decoupled your suite is. On a tightly-coupled codebase (I tested Flask) you only skip ~21%, because a core change legitimately reaches most tests. On a modular one (boltons) it's ~96%. So this helps suites with independent feature areas far more than a small tightly-coupled service.
- Correctness is the scary part: a false negative (skipping a test that should have run) is a broken build that passes green. I ended up writing a mutation test that mutates every covered function and asserts every covering test gets re-selected, to actually back the no-false-negative claim instead of just asserting it.
Questions for people who've run this in anger:
- If you do TIA in CI, how do you handle the map going stale, or the very first run on a brand-new branch where there's no map yet?
- Do you actually gate on it (skip tests in the pipeline), or only use it for ordering/prioritization and still run the full suite eventually?
The tool is pytest-tia (https://github.com/breadMSA/pytest-tia, MIT), but I'm more interested in how others are doing affected-test selection in their pipelines.
3
u/ForkMeJ Jun 21 '26
I'd be careful about hard-gating on selected tests unless you also have a scheduled or merge-to-main full run, because stale maps and missed transitive coverage are exactly the kind of thing that creates a green pipeline and a bad deploy. For branch bootstrap, I'd fall back to full suite until a baseline artifact exists, and I'd probably key that artifact on both commit/ref and test environment so dependency drift doesn't quietly poison it.
2
u/r410b 23d ago
Hey, Really clean implementation! I'm building a JVM equivalent of this Blastradius for Maven/Gradle + JUnit 5 and it's wild how converged the architecture is across ecosystems:
• Same S3/CI split: We landed on the exact same TRACK on trunk -> push map to S3/CI cache -> SELECT on PR with fallback to latest snapshot.
• Same conservative escape hatches: You widen reflection (getattr/eval) to file-level and audit open calls; on the JVM side we had to add a two-edge static invocation fallback for string-dispatched APIs and force full runs on non-source/config changes.
• Same trust model: Pairing daily/scheduled full-suite runs with TIA rather than pretending any dynamic tracer is 100% immune to reflection or subprocesses.
One interesting difference: you went method-level via AST (plus the TYPE_CHECKING / cosmetic diff filter, which is super smart for Python), whereas we stayed at the class level via a java.lang.instrument agent and validated across ~800 commits on Apache repos (averaging ~61% skip). Nice work on the honest benchmarking too!
2
u/sokjon Jun 21 '26
I’ve built a custom tool for Go that does the necessary analysis against changed packages and dependencies in order to provide both changed tests and main/entrypoints.
For Go it generally very feasible to do the full analysis every time, 5-10s to avoid running 5mins of tests is a good compromise.
Correctness and trust is derived from a good testing and integration suite (the ability to create real git scenarios), as well as just having run the tool thousands of times.
2
u/Kazcandra Jun 21 '26
You can clone shallow with a depth to get latest + one commit back
2
u/[deleted] Jun 21 '26
[removed] — view removed comment