Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 changelog/9114.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added :ref:`pythonpath` setting that adds listed paths to `sys.path`.
1 change: 1 addition & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def directory_arg(path: str, optname: str) -> str:
"reports",
*(["unraisableexception", "threadexception"] if sys.version_info >= (3, 8) else []),
"faulthandler",
"pythonpath",
)

builtin_plugins = set(default_plugins)
Expand Down
28 changes: 28 additions & 0 deletions src/_pytest/pythonpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys
from typing import List

import pytest
from pytest import Config
from pytest import Parser


added_paths: List[str]


def pytest_addoption(parser: Parser) -> None:
parser.addini("pythonpath", type="paths", help="Add paths to sys.path", default=[])


@pytest.hookimpl(tryfirst=True)
def pytest_load_initial_conftests(early_config: Config) -> None:
global added_paths
added_paths = [str(p) for p in early_config.getini("pythonpath")]
# `pythonpath = a b` will set `sys.path` to `[a, b, x, y, z, ...]`
for path in reversed(added_paths):
sys.path.insert(0, path)


def pytest_unconfigure() -> None:
global added_paths
for path in added_paths:
sys.path.remove(path)
8 changes: 7 additions & 1 deletion testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,13 @@ def pytest_load_initial_conftests(self):
pm.register(m)
hc = pm.hook.pytest_load_initial_conftests
values = hc._nonwrappers + hc._wrappers
expected = ["_pytest.config", m.__module__, "_pytest.capture", "_pytest.warnings"]
expected = [
"_pytest.config",
m.__module__,
"_pytest.pythonpath",
"_pytest.capture",
"_pytest.warnings",
]
assert [x.function.__module__ for x in values] == expected


Expand Down
72 changes: 72 additions & 0 deletions testing/test_pythonpath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from textwrap import dedent

import pytest
from _pytest.pytester import Pytester


@pytest.fixture()
def file_structure(pytester: Pytester) -> None:
pytester.makepyfile(
test_foo="""
from foo import foo

def test_foo():
assert foo() == 1
"""
)

pytester.makepyfile(
test_bar="""
from bar import bar

def test_bar():
assert bar() == 2
"""
)

foo_py = pytester.mkdir("sub") / "foo.py"
content = dedent(
"""
def foo():
return 1
"""
)
foo_py.write_text(content, encoding="utf-8")

bar_py = pytester.mkdir("sub2") / "bar.py"
content = dedent(
"""
def bar():
return 2
"""
)
bar_py.write_text(content, encoding="utf-8")


def test_one_dir(pytester: Pytester, file_structure) -> None:
pytester.makefile(".ini", pytest="[pytest]\npythonpath=sub\n")
result = pytester.runpytest("test_foo.py")
result.assert_outcomes(passed=1)


def test_two_dirs(pytester: Pytester, file_structure) -> None:
pytester.makefile(".ini", pytest="[pytest]\npythonpath=sub sub2\n")
result = pytester.runpytest("test_foo.py", "test_bar.py")
result.assert_outcomes(passed=2)


def test_module_not_found(pytester: Pytester, file_structure) -> None:
"""Without the pythonpath setting, the module should not be found."""
pytester.makefile(".ini", pytest="[pytest]\n")
result = pytester.runpytest("test_foo.py")
result.assert_outcomes(errors=1)
expected_error = "E ModuleNotFoundError: No module named 'foo'"
result.stdout.fnmatch_lines([expected_error])


def test_no_ini(pytester: Pytester, file_structure) -> None:
"""If no ini file, test should error."""
result = pytester.runpytest("test_foo.py")
result.assert_outcomes(errors=1)
expected_error = "E ModuleNotFoundError: No module named 'foo'"
result.stdout.fnmatch_lines([expected_error])