Skip to content

All mutants reported as "No Tests" when test imports differ from the file-path module name #515

@irajaryan

Description

@irajaryan

Hit this on a real codebase and reduced to a minimal repro. mutmut runs to completion, every mutant is marked "No Tests", but the test suite passes when run directly with pytest and the trampolines actually execute. The early-bail check in run_stats_collection doesn't catch it because hits are being recorded during stats collection, just under keys that no mutant lookup will ever match.

Repro

buggy/
├── pyproject.toml
├── pkg/
│   ├── __init__.py
│   └── foo.py        # def add(a, b): return a + b
└── tests/
    └── test_foo.py   # import foo;  def test_add(): assert foo.add(2, 3) == 5
[tool.mutmut]
source_paths = ["pkg"]

[tool.pytest.ini_options]
pythonpath = ["pkg"]
  • pytest -q -> 2 passed
  • mutmut run -> 3/3 🎉 0 🫥 3 ⏰ 0 🤔 0 🙁 0 🔇 0 🧙 0, 0.00 mutations/second

What's happening

After patching prints into pytest_runtest_teardown and mangled_name_from_mutant_name, the recorded and lookup keys come out as:

RECORD test='tests/test_foo.py::test_add'         hits=['foo.x_add']
RECORD test='tests/test_foo.py::test_is_positive' hits=['foo.x_is_positive']
LOOKUP mutant='pkg.foo.x_add__mutmut_1'           key='pkg.foo.x_add'
LOOKUP mutant='pkg.foo.x_is_positive__mutmut_1'   key='pkg.foo.x_is_positive'

When the test does import foo, __module__ is 'foo', so record_trampoline_hit writes 'foo.x_add'. The mutant name is built from the file path pkg/foo.py, so the lookup key is 'pkg.foo.x_add'. They never match.

The early-bail check around __main__.py:736 sums total recorded associations (2 here), which passes the > 0 gate, so the run continues and every per-mutant lookup quietly returns the empty set.

Workaround

Use qualified imports in tests (from pkg.foo import add) and drop the pythonpath injection. For repos with hyphenated source dirs, also rename to underscore and switch internal imports to relative.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions