Skip to content

Re-run mutants when tests are edited or added - #635

Open
nafiurahman00 wants to merge 1 commit into
boxed:mainfrom
nafiurahman00:fix/invalidate-results-on-test-edits
Open

nafiurahman00 wants to merge 1 commit into
boxed:mainfrom
nafiurahman00:fix/invalidate-results-on-test-edits

Conversation

@nafiurahman00

@nafiurahman00 nafiurahman00 commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #626

Cached results were only reset when the source code changed. Test files don't have function hashes and git_changed_non_py_files() skips all .py files, so after removing an assertion from a test, mutmut run kept reporting the mutants it used to kill as killed. The other direction was broken too: adding a test that kills a survivor didn't re-run it (the case @snapshotpl mentioned).

I went with the targeted approach suggested in the thread rather than rerunning everything on any test change:

  • mutmut now stores a hash of every test file it collected stats from, plus the conftest.py files above them (test_file_hashes in mutmut-stats.json)
  • if a test file is edited or deleted, its tests are removed from the stats so the existing "new tests" path collects them again, and the mutants of the functions they used to cover are reset
  • functions covered by new or edited tests get their survived / no tests results reset. Killed ones are kept, since more coverage can't un-kill a mutant
  • conftest.py can affect every test, so a change there goes through on_dependency_change like the other watched files (warns by default, until the next full run)

Old stats files without test_file_hashes still load, and the hashes are recorded on the next run.

I also changed the "non-Python file(s) changed" message to "dependency file(s) changed" since conftest.py can show up there now, and updated the README section that said Python changes are already tracked per function.

Tested with the script from the issue, with a second test file that gets a stronger assertion:

== cold run
    calc.ops.x_is_adult__mutmut_1: killed
    calc.ops.x_is_adult__mutmut_2: killed
    calc.ops.x_double__mutmut_1: survived
    calc.ops.x_double__mutmut_2: survived
== warm run after editing both tests (before this change: same as the cold run)
    calc.ops.x_is_adult__mutmut_1: survived
    calc.ops.x_is_adult__mutmut_2: survived
    calc.ops.x_double__mutmut_1: killed
    calc.ops.x_double__mutmut_2: killed
== cold run after rm -rf mutants
    (same as the warm run)

I also checked a run with no changes (nothing re-run), editing only one test file (only its functions re-run), and adding a conftest.py with both warn and rerun. Added unit tests in tests/mutation/test_mutation.py. uv run pytest passes (488 passed, 4 skipped) and so does pre-commit run --all-files.

One thing I noticed while testing: deleting a test file doesn't remove its copy from mutants/, so the deleted test still runs. That's a separate bug on main, fixed in #636.

Cached results were only invalidated when the source changed, so
removing an assertion from a test left its mutants as "killed", and a
new test that kills a survivor didn't change anything until mutants/
was deleted.

Store a hash of each test file (and the conftest.py files above it).
When a test file changes, its tests are collected again and the mutants
of the functions they covered are re-run. Functions covered by new
tests get their survived/no tests results reset. conftest.py changes go
through on_dependency_change since they can affect every test.

Fixes boxed#626
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.

Editing a test doesn't invalidate cached results

1 participant