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
3 changes: 3 additions & 0 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ def import_path(
mod = importlib.util.module_from_spec(spec)
sys.modules[module_name] = mod
spec.loader.exec_module(mod) # type: ignore[union-attr]
if path.is_dir() and path.joinpath("__init__.py").exists():
mod.__path__ = [str(path)]
insert_missing_modules(sys.modules, module_name)
insert_missing_modules(sys.modules, module_name)
return mod

Expand Down
21 changes: 21 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,27 @@ def test_parsing_again_fails(self, pytester: Pytester) -> None:
config = pytester.parseconfig()
pytest.raises(AssertionError, lambda: config.parse([]))

def test_import_mode_importlib_with_conftest_and_pythonpath(self, pytester: Pytester):
pytester.makepyfile(tests="__init__.py")
pytester.makepyfile(tests_conftest="# Existence of this file breaks package discovery")
pytester.makepyfile(tests_subpath="__init__.py")
pytester.makepyfile(tests_subpath_helper="# Empty helper file")
test_file_content = textwrap.dedent("""
import tests.subpath.helper
def test_something():
assert True
""")
pytester.makepyfile(tests_subpath_test_something=test_file_content)
pytester.makefile(".ini", pytest="""
[tool:pytest]
pythonpath = .
addopts = --import-mode importlib
""")

result = pytester.runpytest()
# The test should pass indicating that tests.subpath.helper was imported correctly
assert result.ret == 0

def test_explicitly_specified_config_file_is_loaded(
self, pytester: Pytester
) -> None:
Expand Down