CI: select tests by coverage-derived impact map instead of running the full suite on any .py change
tl;dr — CI runs all 315 test modules whenever any
.pyfile changes..github/helper/ci.pyalready collects coverage but discards per-test attribution; recording it in the nightly run yields a{source_file: [test_modules]}map thatroulette.pycan intersect with a PR's changed files. Estimated 31–53% off Integration runner-time and PR feedback from ~9.7 min to ~4.5–6.5 min, with the nightly full run as the safety net.
Problem
server-tests.yml runs the full Python suite whenever any .py file changes. roulette.py only decides whether to run tests, never which tests. A one-line fix to a single doctype controller runs all 315 test modules.
Why not AST / import-graph selection
The obvious approach — build a reverse import graph, run only the test modules that transitively import a changed file — is unsound here. Frappe resolves most call targets at runtime:
| Pattern | Occurrences |
|---|---|
frappe.get_attr / get_module / import_module / get_hooks |
293 |
Dotted-path strings in hooks.py (doc_events, scheduler_events, overrides) |
184 |
@frappe.whitelist endpoints called by dotted path from JS/HTTP |
570 |
frappe.get_doc(...) binding a controller class by doctype name |
1946 |
Plus doctype JSON → controller resolution, Jinja templates, and override_doctype_class. Every one of those is an edge a static analyser cannot see. Missing edges mean silently skipping the test that would have caught the regression — found on develop instead of on the PR. A selector that is usually right is worse than no selector.
Proposal: coverage-derived impact map
Coverage observes what actually executed, so it captures all of the above for free. .github/helper/ci.py already runs the suite under coverage.Coverage(...) — it just discards per-test attribution by writing one aggregate XML.
1. Build the map in the nightly cron (server-tests.yml already has cron: "0 0 * * *" — off the PR critical path)
Add dynamic_context="test_function" to the Coverage(...) construction in .github/helper/ci.py. Invert the resulting .coverage SQLite via CoverageData().contexts_by_lineno() into {source_file: [test_modules]} and publish it as an artifact or actions/cache entry keyed by branch.
2. Select in roulette.py
Intersect the PR's changed .py files with the map, emit the test module list. Escape hatches that force the full suite:
- no map available, or map older than N days
frappe/hooks.py,frappe/__init__.py,frappe/model/**,frappe/database/**- any
.jsondoctype schema change .github/**change (already handled), push builds, and the nightly itself
3. Execute the subset
ParallelTestRunner.get_test_file_list() (frappe/parallel_test_runner.py) takes the list instead of globbing. Per-module invocation already exists in run-indinvidual-tests.yml (bench run-tests --module <x>).
Safety net: the nightly full run. Worst case a stale map lets a regression through for <24h — the same window already accepted for cron-only failures.
Estimated savings
Measured on frappe/frappe, 30 days to 2026-08-23.
Current spend
| Metric | Value | Source |
|---|---|---|
server-tests.yml runs |
1622 | Actions API |
| Completed Integration jobs per run | 0.65 | 43-run sample (rest cancelled or roulette-skipped) |
| Integration jobs / month | ~1056 | |
| Mean job duration | 584 s | 28-job sample |
| Integration runner-time / month | ~10 280 min (171 h) |
Reducible portion — step breakdown of a representative job (run 32529914956):
18s Initialize containers
3s Checkout
105s Environment Setup
432s Run Tests <- 77%, the only selectable part
~3s everything elsebefore_test_setup is only ~5 s of that 432 s, so nearly all of it scales with the number of test files selected. Fixed overhead is ~130 s/job and is untouched.
Reducible pool: 1056 jobs × 454 s ≈ 7 990 runner-min/month (133 h).
PR shape — 120 merged PRs sampled:
| Bucket | Count | Share |
|---|---|---|
No .py/.po at all (roulette already skips) |
55 | 46% |
| Server tests run | 65 | 54% |
| └ touches core/schema/CI → escape hatch, full suite | 13 | 20% of running |
| └ selectable | 52 | 80% of running |
Median changed .py files per PR: 2 (p90: 4, max: 8).
Savings, by how much test time a typical narrow change's coverage closure pulls in (the one number this design cannot predict — it is what the pilot measures):
| Closure size | Saved / month | % of Integration runner-time |
|---|---|---|
| 15% (optimistic) | ~5 430 min (91 h) | 53% |
| 30% (central) | ~4 470 min (75 h) | 43% |
| 50% (conservative) | ~3 200 min (53 h) | 31% |
Cost side: dynamic_context adds roughly 10–30% to the nightly run, ≈ +120 min/month. Net stays strongly positive in every scenario.
Wall clock per PR: Integration job ~9.7 min → ~4.5–6.5 min. This is the real prize. GitHub-hosted minutes are free on public repos, so the case here is contributor feedback latency and queue contention, not the bill. (For reference, if this ever moved to paid runners the central scenario is ~$36/mo at the 2-core rate, ~$72/mo on 4-core.)
Suggested sequencing
- Land the
dynamic_contextchange + map publication alone. Zero behaviour change, and it lets us measure the actual closure size before committing to selection. - Add selection to
roulette.pybehind a label or env flag, run it in shadow mode (log what it would have skipped, still run everything) for a week. - Enable, keeping the nightly full run as the net.
Step 1 is worth doing regardless — without it, the closure-size row above stays a guess.
Cheaper interim option
A path heuristic in roulette.py, no coverage at all: if every changed .py is .../doctype/<dt>/<dt>.py, run only the matching test_<dt>.py; otherwise full CI. Covers the single-doctype-controller PR (a large share of the 80% selectable bucket) and is trivially auditable. Strictly worse coverage than the map, but ~20 lines.
Source: frappe/frappe