r/devops • • 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:

  1. 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.
  2. 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.
  3. 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.

6 Upvotes

18 comments sorted by

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.

→ More replies (1)

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!

1

u/breadMSA 18d ago

Thanks, and great to see Blastradius! The convergence is striking. Two questions: on the JVM side, did per-test coverage ever silently drop contexts the way coverage.py's sysmon core did for me? (It kept only the first test per line and inflated my skip rate from ~21% to 73%.) And did you ever try method-level on the JVM? Curious whether the extra skip over class-level was worth the complexity. Happy to cross-link the projects.

2

u/r410b 17d ago

Yes, same bug in different costume :) The JVM loads each class only once, so a simple tracker credits a class only to the first test that loaded it. Every later test that reuses it, like a shared Spring bean or a singleton, gets nothing recorded. On apache/shenyu, a real change selected 0 of 3,627 tests. Like yours, the skip rate looked great and was wrong.

Our fix was to inject a small callback into every method of the project's classes when they load. Each test that actually uses a class now gets credited, not just the first. Anything we can't instrument goes on a "run everything if this changes" list. One gap remained: code reached by name lookup rather than a direct call. jsoup's parser hit that, and we only found it through mutation testing. So I'm with you on using mutation testing to back the no-false-negative claim.

Method-level: never tried it, on purpose. Class-level is what Ekstazi does, and as I recall their paper found it faster overall, because the tracking is so much cheaper. On the JVM, mapping a source change onto methods is also tricky: field initializers end up inside constructors, lambdas become hidden methods. Each of those is a chance to skip a test you shouldn't. The cost is big classes: one edit to a huge parser reruns every test that touches it. I'd rather run extra tests than risk skipping one that should have failed. But I've never measured how much more method-level would actually skip, so I can't say if it's worth it. Did you ever compare function-level against file-level on Flask or boltons?

Thank you :) Happy to cross-link too !

2

u/breadMSA 16d ago

Great question, so I measured it instead of guessing. Same harness as my benchmarks: record on each commit's parent, then select twice, once by function and once by file. Both keep the same safety rules (module-level edits and reflection-flagged files already widen to file).

boltons (20 commits): almost a wash. Function-level ran 699 tests in total, file-level ran 750. Only 5 of 20 commits differed. Each module has its own test file, so the file was already a good proxy.

Flask (14 commits): your "big class" cost, exactly. On most commits the two modes were identical. But on 3 commits a function-level edit to a large core file (app.py and friends) selected 3, 138 and 9 tests, where file-level selected 429, 430 and 284. That's 26% fewer tests over the window (2,840 vs 3,833).

So my takeaway matches your intuition: the extra precision is worth it in direct proportion to how big and central your files are. On a modular codebase, file/class-level gets you about 95% of the way at a fraction of the risk. Python makes the mapping easier than the JVM (no hidden lambda methods or synthesized initializers; a function body is just an AST span), which is probably why I could afford it. Where I can't trust the edges (getattr by computed name, importlib, eval), I already fall back to file-level, which is more or less your "run everything if this changes" list at a finer grain.

Still to do: a mutation pass at both granularities. A finer map has more ways to miss a test, so that's the number I'd want before calling it free.

Full tables: https://github.com/breadMSA/pytest-tia/blob/main/benchmark/RESULTS-granularity.md. Blastradius is now linked from the README too.

2

u/r410b 9d ago

Amazing! Really similar approaches :) now I'm struggling with the promotion and user adoption. With 25+ stars it should be easier for you to get listed on awesome-lists. I added you on my main README. Thanks!

2

u/breadMSA 9d ago

Thanks, and thanks for adding me to your README! Blastradius is linked from mine too. Good tip on awesome-lists, I just opened a PR to awesome-python-testing. Adoption is the hard part for me as well, so if either of us finds something that actually brings in users, let's compare notes.

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/breadMSA Jun 21 '26

Yeah, Go's static package graph is exactly what makes full analysis cheap enough to run every time, that's the luxury Python doesn't have, so I had to go the runtime-coverage route instead of static. And +1 on trust coming from real git scenarios run thousands of times; that's basically why I leaned on per-commit replays plus a mutation test rather than just asserting it's correct.

2

u/Kazcandra Jun 21 '26

You can clone shallow with a depth to get latest + one commit back

1

u/breadMSA Jun 21 '26

Yeah for a single commit --depth 2 does the job. It falls apart though once the PR has more than one commit, or you are diffing against the merge-base which can be way more than one commit back. You would have to know the right depth up front and that varies per PR. Baking the function tables into the map is what lets it resolve the diff no matter how deep the base is, without guessing a depth or fetching history. AFAIK that is the part shallow depth does not cover.

2

u/sokjon Jun 21 '26

As a squash merge shop, this is one thing which is simplified. There’s only ever one commit to consider :-)

→ More replies (1)
→ More replies (1)

2

u/[deleted] Jun 21 '26

[removed] — view removed comment

1

u/breadMSA Jun 21 '26

Honestly tia does not catch that, and I don't think any coverage-diff tool does. If a dependency changes behavior without your own source changing, there is nothing in the diff to trace back to. A lockfile bump is in the diff, but since no test actually reads the lockfile at runtime, tia maps nothing to it and would happily skip everything, which is exactly the false negative you hit.

So the honest answer is you have to treat that class of change as a full-suite trigger. Right now I would just wire "if the lockfile or requirements file changed, run everything" into CI, and baking that in as a built-in escalation rule is on my list. Your skepticism about how complete the dep graph really is is the right instinct, that is the real blind spot for this whole approach, not just mine.

This is basically the same reason the full-suite backstop ForkMeJ mentioned matters. Selection handles what it can see, the backstop catches what it cannot.