Skip to content

[WIP] Run only the tests that reach the line a mutant changes - #672

Draft
MartinThoma wants to merge 2 commits into
boxed:mainfrom
MartinThoma:run-only-tests-that-reach-the-line
Draft

MartinThoma wants to merge 2 commits into
boxed:mainfrom
MartinThoma:run-only-tests-that-reach-the-line

Conversation

@MartinThoma

Copy link
Copy Markdown
Contributor

This is WIP - it seems to work for pypdf and brings down time significantly. Before AI, I would have opened an issue to discuss if this type of change is desired. With AI, it's easier to draft something that works / keep CI green and then discuss about it :-)

The idea of this change is to speed up mutmut run by executing just those parts of the test suite that have a chance of killing a mutant - preferably those first, that are fast.

The change

On Python 3.12 and later, the stats run records with sys.monitoring which lines of each mutated function every test runs, in the fork runner, the fork server and pytest-xdist workers. A mutant then gets only the tests of its function that run the statement it changes. A mutant on a line that no test runs is reported as "no tests" without starting a worker, and is run again once a new test reaches its line.

A mutant still gets every test of its function on older Pythons, for a mutant in the signature, and when the function's source changed since its lines were recorded.

On pypdf, the median mutant gets 20 tests instead of 57, and the test time of a surviving mutant falls by 42 %. On pagerange.py and _cmap.py, main and this change kill the same 980 mutants.

MartinThoma and others added 2 commits December 3, 2026 11:37
On Python 3.12 and later, the stats run records with sys.monitoring which
lines of each mutated function every test runs, in the fork runner, the
fork server and pytest-xdist workers. A mutant then gets only the tests of
its function that run the statement it changes. A mutant on a line that no
test runs is reported as "no tests" without starting a worker, and is run
again once a new test reaches its line.

A mutant still gets every test of its function on older Pythons, for a
mutant in the signature, and when the function's source changed since its
lines were recorded.

On pypdf, the median mutant gets 20 tests instead of 57, and the test time
of a surviving mutant falls by 42 %. On pagerange.py and _cmap.py, main and
this change kill the same 980 mutants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No test calls Color.to_index with Color.BLUE, so on Python 3.12 and later
no test reaches the line of its third return and the mutant gets "no
tests" instead of surviving. Older Pythons have no sys.monitoring and keep
the old verdict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@boxed

boxed commented Dec 3, 2026

Copy link
Copy Markdown
Owner

The description of this seems to be to do what mutmut already does. Is the issue that the current code that tries to only select tests that hit the code doesn't work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants