From 1a26caa411b45ace7783c6fb03ed41f3102e4cbd Mon Sep 17 00:00:00 2001 From: agustin18 Date: Thu, 19 Nov 2026 19:13:36 +0000 Subject: [PATCH] fix: drop collection-time hits from stats attribution Trampolined functions called while pytest imports test and source modules were left in state()._stats and credited to whichever test ran first. A function only called at import time therefore looked covered, so its mutants were reported as survived instead of no tests. Clear the buffer in pytest_collection_finish, which runs after collection and before the first test. --- src/mutmut/runners/harness.py | 8 ++++++++ tests/runners/test_harness.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/mutmut/runners/harness.py b/src/mutmut/runners/harness.py index ed4bc9db..3c2120bf 100644 --- a/src/mutmut/runners/harness.py +++ b/src/mutmut/runners/harness.py @@ -146,6 +146,14 @@ def _pytest_args_regular_run(self, tests: Iterable[str]) -> list[str]: def run_stats(self, *, tests: Iterable[str]) -> int: class StatsCollector: + # noinspection PyMethodMayBeStatic + def pytest_collection_finish(self, session: Any) -> None: + # Importing test and source modules during collection calls + # trampolined functions, but that is not evidence a test calls + # them. Drop those hits instead of crediting the first test. + unused(session) + state()._stats.clear() + # noinspection PyMethodMayBeStatic def pytest_runtest_logstart(self, nodeid: str, location: Any) -> None: state().duration_by_test[nodeid] = 0 diff --git a/tests/runners/test_harness.py b/tests/runners/test_harness.py index 5fa3f7bf..db2f3786 100644 --- a/tests/runners/test_harness.py +++ b/tests/runners/test_harness.py @@ -3,10 +3,12 @@ import json from collections import defaultdict from pathlib import Path +from types import SimpleNamespace import pytest from mutmut.runners.harness import ListAllTestsResult +from mutmut.runners.harness import PytestRunner from mutmut.state import state STATS_FILE = Path("mutants/mutmut-stats.json") @@ -69,3 +71,33 @@ def test_nothing_obsolete_leaves_stats_file_untouched(self, capsys): assert dict(state().tests_by_mangled_function_name) == {"pkg.foo.x_add": {"tests/test_foo.py::test_add"}} assert capsys.readouterr().out == "" assert not STATS_FILE.exists() + + +class TestStatsCollectorCollectionHits: + def test_hits_recorded_during_collection_are_not_credited_to_the_first_test(self, monkeypatch): + captured = [] + + def fake_execute_pytest(self, params, **kwargs): + captured.extend(kwargs["plugins"]) + return 0 + + monkeypatch.setattr(PytestRunner, "execute_pytest", fake_execute_pytest) + state()._stats.clear() + + PytestRunner().run_stats(tests=[]) + (collector,) = captured + + # Importing a module during collection calls a trampolined function. + state()._stats.add("pkg.mod.x_table") + collector.pytest_collection_finish(None) + + # Only hits recorded while the test runs belong to it. + item = SimpleNamespace(nodeid="tests/test_mod.py::test_first") + collector.pytest_runtest_logstart(item.nodeid, location=None) + state()._stats.add("pkg.mod.x_double") + collector.pytest_runtest_teardown(item, None) + + assert dict(state().tests_by_mangled_function_name) == { + "pkg.mod.x_double": {"tests/test_mod.py::test_first"}, + } + assert state()._stats == set()