Use -mpytest when invoking pytest in pytester

This commit is contained in:
Anthony Sottile 2018-07-07 17:18:44 -07:00
parent aa9d1ad2eb
commit 42bbb4fa8a
3 changed files with 7 additions and 8 deletions

1
changelog/742.bugfix.rst Normal file
View File

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

View File

@ -23,11 +23,6 @@ from _pytest.main import Session, EXIT_OK
from _pytest.assertion.rewrite import AssertionRewritingHook from _pytest.assertion.rewrite import AssertionRewritingHook
PYTEST_FULLPATH = os.path.abspath(pytest.__file__.rstrip("oc")).replace(
"$py.class", ".py"
)
IGNORE_PAM = [ # filenames added when obtaining details about the current user IGNORE_PAM = [ # filenames added when obtaining details about the current user
u"/var/lib/sss/mc/passwd" u"/var/lib/sss/mc/passwd"
] ]
@ -1029,9 +1024,7 @@ class Testdir(object):
print("couldn't print to %s because of encoding" % (fp,)) print("couldn't print to %s because of encoding" % (fp,))
def _getpytestargs(self): def _getpytestargs(self):
# we cannot use `(sys.executable, script)` because on Windows the return (sys.executable, "-mpytest")
# script is e.g. `pytest.exe`
return (sys.executable, PYTEST_FULLPATH) # noqa
def runpython(self, script): def runpython(self, script):
"""Run a python script using sys.executable as interpreter. """Run a python script using sys.executable as interpreter.

View File

@ -394,3 +394,8 @@ class TestSysPathsSnapshot(object):
assert getattr(sys, path_type) == original_data assert getattr(sys, path_type) == original_data
assert getattr(sys, other_path_type) is original_other assert getattr(sys, other_path_type) is original_other
assert getattr(sys, other_path_type) == original_other_data assert getattr(sys, other_path_type) == original_other_data
def test_testdir_subprocess(testdir):
testfile = testdir.makepyfile("def test_one(): pass")
assert testdir.runpytest_subprocess(testfile).ret == 0