Video 8 min
pytest-testmon: Run Only the Tests Affected by Your Code Change
pytest-testmon runs only the tests a change can reach, from a coverage record, not a guess. Adding it to a 2,248-test Django suite, the wrapper it needs, and a Python 3.14 trap.
Narrated in English Subtitles: English, 日本語, 한국어, Tiếng Việt, 简体中文
Transcript
A feature request is usually small. One screen, one endpoint, one rule. But every change waited on my whole test suite, checking things it never touched. So I set up a tool to run only the tests a change can reach. The first numbers looked wonderful.
A handful of tests. A few seconds. All green. They were wrong. Half the tests I'd broken never ran.
And nothing on screen said so.
This is how I got that time back, and what nearly fooled me. The tool is pytest-testmon, a free, open-source pytest plugin for test impact analysis. Using coverage.py, it records which blocks of code each test runs. Next time, it runs only the tests whose blocks changed, plus last time's failures. No git involved.
I added it to my marketplace app this week. And the reason wasn't speed for its own sake. The suite is two thousand, two hundred and forty-eight Django tests. Every edit ran all of it. When something unrelated failed, or the run just took its time, the feature waited.
It was being held up by circumstances outside the feature request, and the thing holding it up was my own test suite. The alternative was guessing which test file covers the change. Quick, but not precise. And an untested change I ship is a feature request I'll be doing twice. I wanted to stay agile without getting sloppy: wait for exactly the tests a change can reach, from a record, not a guess.
The full suite still runs, in CI.
testmon splits each Python file into blocks: the top level, each class, each function. And it stores a checksum of each one. In this pricing module, a test that only calls subtotal depends on the top level and subtotal's body. Rewrite discount, and that test won't rerun. Change one character inside subtotal, though, and it runs again, even if the result can't change.
No static analysis. No guessing from imports. testmon doesn't try to be clever about side effects. If a block you executed changed, you run. It watches the surroundings too: the Python version, packages, some environment variables.
Upgrade Django, and in a Django app, everything reruns. But it only sees Python a test executes. Not templates, JSON files or network services. Not migrations or settings, which run before any test starts. My suite had more: shared fixtures, a built web bundle, a generated site.
Change one, and testmon will happily tell you nothing needs to run.
Setup is mostly simple. Install it, get the suite running under pytest, and let testmon record every test once. Here, that took about twelve minutes. After that, with nothing changed, it runs nothing. But on Python three point fourteen or later, one setting matters more than anything else here.
I'll come back to it. What makes it safe every day is a wrapper script. Something testmon can't trace changed? Run everything. A new migration?
Rebuild the database. Otherwise, testmon chooses. My first version also ran everything when the lock file changed, firing the slow path most days. testmon already tracks package versions itself. Timed, start-up included: with nothing changed, about a second.
One edit to the code behind a home-screen banner ran seventeen tests. Eight and a half seconds. For an everyday edit, that's about twenty-five times faster than the parallel suite. Touch a core module every test runs, and most of it reruns. That's right, not a failure.
The cost is the fallback: a full, single-process recording again. So keep the files that trigger it as few as honestly possible.
Those weren't my first numbers. My first were better. An edit ran just six tests. And they were wrong. From Python three point fourteen, coverage.py's new default core reports a line only the first time a process runs it.
So testmon credited each line to just one test. A second project that copied the setup caught it. Back here, I broke one line on purpose. Plain pytest reported eight failing tests. testmon selected four.
The other four never ran, and nothing in the output said they'd been left out. The fix is one line, putting coverage back on its classic C tracer. Re-recorded, the same kind of edit ran all eleven tests it broke. That's the lesson I'd pass on first. A test selector that selects too little looks exactly like success: fewer tests, a faster run, all green.
So break something shared on purpose, and check that the plain suite and the selector fail the same tests. Again, whenever Python or coverage.py changes version. Now a coding agent, Claude Code in my case, makes many of the small changes. Facing the whole suite, it had my two bad options: wait, or guess. Now the wrapper is its default: the same precise tests I get, in seconds.
None of this is AI-specific. It just matters more when whatever makes the changes makes a great many of them.
testmon has no CI mode. And a record cached between runners would be blind in all the same ways. So CI does the same at file level: a small job diffs each push and decides whether it touched the Python API, the mobile app, both, or neither. A docs-only push now runs nothing, and a push to the app alone skips the Django suite. It's cruder, but it fails in the safe direction.
If anything on the Python side changed, the whole suite runs.
So, is it worth it? For a suite that takes minutes, yes. One dependency, one flag, one environment variable. The point was never the minutes themselves: it's that a change gets judged by the tests it can affect, so a small request stays small. With the C tracer set, the worst it does is run too much.
Without it, it can run too little and stay green. The laptop loop is where testmon belongs. CI still runs the whole suite when the Python side changes, and that's the run I trust before a deploy. The full post, with the setup steps, the wrapper script and links, is on jared.lynskey.co.nz.



