I Turned a Repo With Almost No Code Into an 11-Minute Test Run
Yesterday someone looked at a pipeline run for a 12-line change and asked a fair question: why does a repository that should hold barely any code spend more than ten minutes running Python tests?
The answer is my fault, along with the people who reviewed my work. This post covers what I built, why it got so slow, and what I’m changing.
Some background
Our top-level repository doesn’t hold application code. It does hold one real program: a template renderer that generates every component repository from a set of shared template kits. That program is close to 8,000 lines. Its test suite is over 10,000 lines.
That suite grew over about seven weeks and roughly 250 commits. It now accounts for about 3,000 of the repository’s 4,200 tests. The other 1,200 or so are small, fast tests for the gate scripts and the CI library.
So the person asking was half right. The repository has real code to test. The problem is how those tests are written and when they run.
What I did wrong
1. Too many tests clone a whole repository
The suite has a good session-scoped fixture. It renders each repository once from the template, and every test that only needs to read a render can share that output. That part works.
Next to it, I wrote a per-test fixture for tests that need to change the template:
@pytest.fixture
def kit_copy(kit, tmp_path):
"""A clone of the session's copy, for a test that changes the kit."""
clone = tmp_path / "repo"
git(["clone", "--quiet", str(kit.source), str(clone)])
return clone
Each of these tests clones the full template tree, commits to it, and starts a separate render process, all to check a single difference. 185 tests use this fixture. On top of the run time, all the cloned and rendered trees once took up most of a gigabyte of disk on the CI machines. We fixed the disk usage and never asked why the tests needed that much disk in the first place.
None of those tests needed a full clone. They each change one file. A per-test copy of that one file, or a template variant rendered once per session and checked many times, would have covered almost all of them.
2. The test gate runs for every change
When we set up path-based gate selection, I set the trigger for the test gate to “every path, minus a few exclusions.” In practice that means a change to a deployment manifest, a pipeline file, a lock file, or a gate script runs all 3,000 renderer tests.
Only changes to the renderer itself, its templates, or its config can make those tests fail. Everything else pays about 11 minutes for nothing. Of the roughly 30 validations since path selection landed, one actually changed the renderer. The rest were pointer bumps, pipeline edits, manifest changes, and issue closes, and every one of them paid the full cost.
Both test trees also run as a single pytest invocation, so there was no way to select one without the other.
3. I never added up the cost
Our testing standard says this directly: when thousands of tests pay a fixture cost like a clone, a process spawn, or a database copy, that cost is the suite’s run time, and the number of tests paying it is what a review should look at.
I knew that rule and didn’t apply it. Each commit added a few tests, and each one looked cheap by itself. Nobody, including me, asked how long the whole suite would take after the next commit. A suite becomes three quarters of a repository’s tests one reasonable-looking commit at a time.
What it cost
About 11 minutes of every pipeline run and every local make gate in this repository. That’s nearly 20% of a full validation, and for almost every change those tests could not have failed.
What I should have done, and what I’m doing now
- Split the gate by test tree. Renderer tests run when the renderer, its templates, or its config change. CI library tests run when the CI library changes. That can be one pytest invocation with a path argument per tree, or two separate gates. Either way, each is triggered by the files it actually depends on. We already learned this lesson for gate selection in general, and a catch-all
*trigger goes against it. - Keep whole-repo clones as a rare exception. A test that changes the template should change one file in a per-test copy of that file. If a test really needs a full render, render it once per template variant at session scope and run many checks against it, the way the existing shared fixture already does. Any remaining full-clone test needs a written reason.
- Review the suite as a whole. For any change that adds tests behind an expensive fixture, I’ll state the change in total run time, not just the number of tests. When one suite is most of a repository’s tests, it gets looked at as a whole, not only through its newest diff.
Lessons
- A gate that covers several unrelated test trees is really several gates. Each tree’s tests should run only when that tree’s inputs change. A wildcard trigger runs everything for a change to anything.
- Price a per-test fixture by how many tests use it. Use session scope for anything tests only read, and the smallest possible copy for anything they change. A full clone or render per test should be rare and justified.
- Small commits can still produce a slow suite. The run-time question applies to every commit that grows the suite, including the 251st.
I’m sorry for the hours this has cost everyone waiting on pipelines. The gate split is first, since it removes the cost from almost every change right away. Rewriting the clone-based tests comes next.