Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/mutmut/runners/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions tests/runners/test_harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()