diff --git a/changelog/14514.bugfix.rst b/changelog/14514.bugfix.rst new file mode 100644 index 00000000000..9d426952acd --- /dev/null +++ b/changelog/14514.bugfix.rst @@ -0,0 +1 @@ +Dots in test module filenames are now escaped with underscores to prevent ``pytest`` from misinterpreting them as package notation when computing the module name. diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 291bdf4ecbf..8024092af8e 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -931,6 +931,7 @@ def compute_module_name(root: Path, module_path: Path) -> str | None: return None if names[-1] == "__init__": names.pop() + names = [p.replace(".", "_") for p in names] return ".".join(names) diff --git a/testing/test_pathlib.py b/testing/test_pathlib.py index bd85b7e8fb4..5d4c85bd6c0 100644 --- a/testing/test_pathlib.py +++ b/testing/test_pathlib.py @@ -1734,6 +1734,12 @@ def test_compute_module_name(tmp_path: Path) -> None: == "src.app.bar" ) + assert compute_module_name(tmp_path, tmp_path / "foo.test.py") == "foo_test" + assert ( + compute_module_name(tmp_path, tmp_path / "src/app/foo.test.py") + == "src.app.foo_test" + ) + def validate_namespace_package( pytester: Pytester, paths: Sequence[Path], modules: Sequence[str]