Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore sys.path after importing test modules #338

Merged
merged 2 commits into from
Apr 3, 2020
Merged
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
4 changes: 4 additions & 0 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ def _importtestmodule(self) -> ModuleType:
# we assume we are only called once per module
importmode = self.config.getoption("--import-mode")
fspath = self.fspath
old_sys_path = sys.path[:] if importmode != "importlib" else None
try:
mod = fspath.pyimport(ensuresyspath=importmode)
except SyntaxError:
Expand Down Expand Up @@ -554,6 +555,9 @@ def _importtestmodule(self) -> ModuleType:
"or @pytest.mark.skipif decorators instead, and to skip a "
"module use `pytestmark = pytest.mark.{skip,skipif}."
)
finally:
if old_sys_path:
sys.path[:] = old_sys_path
self.config.pluginmanager.consider_module(mod)
return mod

Expand Down
15 changes: 15 additions & 0 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ def test():
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)

@pytest.mark.parametrize("import_mode", ("append", "prepend", "importlib"))
def test__importtestmodule_restores_sys_path(self, import_mode, testdir):
p1 = testdir.makepyfile(
"""
import sys

def test():
assert sys.path == {!r}
""".format(
sys.path
)
)
reprec = testdir.inline_run("--import-mode={}".format(import_mode), str(p1))
reprec.assertoutcome(passed=1)

def test_syntax_error_in_module(self, testdir):
modcol = testdir.getmodulecol("this is a syntax error")
pytest.raises(modcol.CollectError, modcol.collect)
Expand Down
1 change: 1 addition & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ def test_multitask_job():
multitask_job()
"""
)
testdir.syspathinsert()
result = testdir.runpytest(p1, "--tb=long")
result.stdout.fnmatch_lines(
[
Expand Down
1 change: 1 addition & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ def test_loader():
assert file.reloaded()
""",
)
testdir.syspathinsert()
result = testdir.runpytest()
result.stdout.fnmatch_lines(["* 1 passed*"])

Expand Down
4 changes: 3 additions & 1 deletion testing/test_monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
from _pytest.monkeypatch import MonkeyPatch
from _pytest.pytester import Testdir


@pytest.fixture
Expand Down Expand Up @@ -318,7 +319,8 @@ def f():
)


def test_importerror(testdir):
def test_importerror(testdir: Testdir) -> None:
testdir.syspathinsert()
p = testdir.mkpydir("package")
p.join("a.py").write(
textwrap.dedent(
Expand Down
1 change: 1 addition & 0 deletions testing/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def test_a():
"""
)

testdir.syspathinsert()
reprec = testdir.inline_run()

reports = reprec.getreports("pytest_runtest_logreport")
Expand Down