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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ Mark Abramowitz
Mark Dickinson
Mark Vong
Marko Pacak
marko1olo
Markus Unterwaditzer
Martijn Faassen
Martin Altmayer
Expand Down
1 change: 1 addition & 0 deletions changelog/14514.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed importlib-mode module names for dotted test filenames such as ``foo.test.py`` so they are imported as valid Python module names.
1 change: 1 addition & 0 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ def compute_module_name(root: Path, module_path: Path) -> str | None:
return None
if names[-1] == "__init__":
names.pop()
names = [x.replace(".", "_") for x in names]
return ".".join(names)


Expand Down
38 changes: 38 additions & 0 deletions testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,41 @@ def test_resolve_pkg_root_and_module_name(
"app.core.models",
)

def test_resolve_pkg_root_and_module_name_dotted_filename(
self, tmp_path: Path
) -> None:
(tmp_path / "pkg").mkdir()
(tmp_path / "pkg/__init__.py").touch()
foo_test_py = tmp_path / "pkg/foo.test.py"
foo_test_py.touch()

assert resolve_pkg_root_and_module_name(foo_test_py) == (
tmp_path,
"pkg.foo_test",
)

def test_import_path_importlib_dotted_filename_in_package(
self, tmp_path: Path
) -> None:
(tmp_path / "pkg").mkdir()
(tmp_path / "pkg/__init__.py").write_text("", encoding="ascii")
(tmp_path / "pkg/foo.py").write_text("value = 1\n", encoding="ascii")
foo_test_py = tmp_path / "pkg/foo.test.py"
foo_test_py.write_text(
"import pkg.foo\nimported_value = pkg.foo.value\n",
encoding="ascii",
)

mod = import_path(
foo_test_py,
mode="importlib",
root=tmp_path,
consider_namespace_packages=False,
)

assert mod.__name__ == "pkg.foo_test"
assert mod.imported_value == 1

def test_insert_missing_modules(
self, monkeypatch: MonkeyPatch, tmp_path: Path
) -> None:
Expand Down Expand Up @@ -1733,6 +1768,9 @@ def test_compute_module_name(tmp_path: Path) -> None:
compute_module_name(tmp_path, tmp_path / "src/app/bar/__init__.py")
== "src.app.bar"
)
assert compute_module_name(tmp_path, tmp_path / "src/app/foo.test.py") == (
"src.app.foo_test"
)


def validate_namespace_package(
Expand Down
Loading