Skip to content

Commit d4faa40

Browse files
authored
Merge pull request #3667 from asottile/avoid_sys_path_pollution_742
Use -mpytest when invoking pytest in pytester
2 parents 12c5b61 + 42bbb4f commit d4faa40

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

changelog/742.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Invoke pytest using ``-mpytest`` so ``sys.path`` does not get polluted by packages installed in ``site-packages``.

src/_pytest/pytester.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
from _pytest.assertion.rewrite import AssertionRewritingHook
2424

2525

26-
PYTEST_FULLPATH = os.path.abspath(pytest.__file__.rstrip("oc")).replace(
27-
"$py.class", ".py"
28-
)
29-
30-
3126
IGNORE_PAM = [ # filenames added when obtaining details about the current user
3227
u"/var/lib/sss/mc/passwd"
3328
]
@@ -1029,9 +1024,7 @@ def _dump_lines(self, lines, fp):
10291024
print("couldn't print to %s because of encoding" % (fp,))
10301025

10311026
def _getpytestargs(self):
1032-
# we cannot use `(sys.executable, script)` because on Windows the
1033-
# script is e.g. `pytest.exe`
1034-
return (sys.executable, PYTEST_FULLPATH) # noqa
1027+
return (sys.executable, "-mpytest")
10351028

10361029
def runpython(self, script):
10371030
"""Run a python script using sys.executable as interpreter.

testing/test_pytester.py

+5
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,8 @@ def test_preserve_container(self, monkeypatch, path_type):
394394
assert getattr(sys, path_type) == original_data
395395
assert getattr(sys, other_path_type) is original_other
396396
assert getattr(sys, other_path_type) == original_other_data
397+
398+
399+
def test_testdir_subprocess(testdir):
400+
testfile = testdir.makepyfile("def test_one(): pass")
401+
assert testdir.runpytest_subprocess(testfile).ret == 0

0 commit comments

Comments
 (0)