diff --git a/changelog/742.bugfix.rst b/changelog/742.bugfix.rst new file mode 100644 index 000000000..51dfce972 --- /dev/null +++ b/changelog/742.bugfix.rst @@ -0,0 +1 @@ +Invoke pytest using ``-mpytest`` so ``sys.path`` does not get polluted by packages installed in ``site-packages``. diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index ce1c8ea1c..7c9c09b1c 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -23,11 +23,6 @@ from _pytest.main import Session, EXIT_OK 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 u"/var/lib/sss/mc/passwd" ] @@ -1029,9 +1024,7 @@ class Testdir(object): print("couldn't print to %s because of encoding" % (fp,)) def _getpytestargs(self): - # we cannot use `(sys.executable, script)` because on Windows the - # script is e.g. `pytest.exe` - return (sys.executable, PYTEST_FULLPATH) # noqa + return (sys.executable, "-mpytest") def runpython(self, script): """Run a python script using sys.executable as interpreter. diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 195f2c7f1..9776a8659 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -394,3 +394,8 @@ class TestSysPathsSnapshot(object): assert getattr(sys, path_type) == original_data assert getattr(sys, other_path_type) is original_other 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