r/Python • • Oct 22 '26

Discussion Timed every test in my pytest suite: 3% of the tests ate 77% of the time

Some context first: I'm one person building an open-source research tool, and most of the code is written by AI coding agents (Claude Code and Codex) running in parallel. They write code quickly. What slowed everything down was the test suite: on one recent change the merge check selected 406 test files and took just over four hours, and the agents sat idle the whole time.

So I pulled together every timing the suite had already recorded (old JUnit XML and --durations output) and summed it per test. That covers about 9,000 test IDs seen across past runs, around 6,800 of which still exist.

243 of them, under 3%, took 77% of the summed time. Most of those build a whole workspace and run an end-to-end day of the app. Also, not a single test qualified as "small" in the Google sense (no disk, no network), because one autouse fixture asks for tmp_path on every test.

So I'm moving to Google's small/medium/large split (the 2010 Testing Blog post). Merge checks will run small and medium only, and the large ones go to release candidates and a nightly run. After that I want per-size timeouts with pytest-timeout, so a "medium" test that slowly grows into a large one fails instead of quietly eating everyone's time.

Two mistakes I made along the way:

The first labelling pass went by names. Anything with "golden" or "first_use" in it got marked large. When someone actually read the bodies, 28 of the 54 groups were fast in-memory checks. They'd have dropped out of merge runs entirely. Label by fixtures and what the test does, not the name.

The other one was worse. I select tests for a merge by following imports from the changed files. My script followed imports through the package but never through shared test helpers or conftest. For one change to a common helper it selected 6 test files; a selector that follows those edges selected 405. I'd been running a fast gate that was mostly blind to helper changes.

Next I'm trying pytest-testmon for coverage-based selection, running it next to the import-based one on real diffs before I trust it on its own.

Curious how others handle this. Do you split by size, or just throw more CI machines at it?

0 Upvotes

9 comments sorted by

21

u/Cool_Swimming4417 Oct 22 '26

Write better tests and stop making Reddit posts with AI to farm karma

0

u/Massive_Distance_616 Oct 23 '26

The tmp_path autouse issue is fixable without changing individual tests. Remove tmp_path from the autouse fixture and request it explicitly only in tests that need filesystem access. If shared setup genuinely needs a temporary directory, use tmp_path_factory in a session-scoped fixture instead. Tests that don’t need disk stop inheriting the dependency silently that’s what actually lets you have small tests in the Google sense.

On pytest-testmon: the thing to watch before trusting it alone is that it tracks coverage per test, so it’ll miss a test that should rerun if the test wasn’t covering the changed path in the run that built the map. Your import-following selector has the opposite failure mode, so running them side by side and diffing the selections is the right call. For any diff where testmon selects fewer tests than your importer, spot-check a sample you’re looking for false negatives, not false positives.

One thing that can bite you in a multi-agent setup: testmon writes to a .testmondata SQLite file. If agents are branching and rebasing frequently, that file drifts or goes stale. In CI you need to cache and restore it per commit parent, or coverage-based selection quietly starts missing regressions.

Also worth including conftest changes in your validation cases. A selector can look solid on ordinary source-file diffs while having a blind spot around test infrastructure you want evidence it handles shared fixture changes before you trust it on its own.

-1

u/Immediate_Wheel1953 Oct 22 '26

We split by size too, and per-tier timeouts were what made it stick for us. My stumble was doing it with one job-level timeout at first: a slow test with a leaking DB connection passed locally but died around minute nine in CI, and it looked flaky for weeks before I realised it was the box, not the test. One thing that actually worked was making the large tier slightly annoying to stay in, ours needs a one-line reason next to the marker. Nobody wants to write the reason, so tests magically shrink.

-3

u/lxl2008 Oct 22 '26

Great idea! I might try requiring a "reason" from my AI agents to see if even LLMs get too lazy to write large tests.