diff --git a/changelog/6213.improvement.rst b/changelog/6213.improvement.rst new file mode 100644 index 000000000..735d4455f --- /dev/null +++ b/changelog/6213.improvement.rst @@ -0,0 +1 @@ +pytester: the ``testdir`` fixture respects environment settings from the ``monkeypatch`` fixture for inner runs. diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index f44a69a95..97ad953a3 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -547,7 +547,8 @@ class Testdir: # Environment (updates) for inner runs. tmphome = str(self.tmpdir) - self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome} + mp.setenv("HOME", tmphome) + mp.setenv("USERPROFILE", tmphome) def __repr__(self): return "".format(self.tmpdir) @@ -853,12 +854,6 @@ class Testdir: plugins = list(plugins) finalizers = [] try: - # Do not load user config (during runs only). - mp_run = MonkeyPatch() - for k, v in self._env_run_update.items(): - mp_run.setenv(k, v) - finalizers.append(mp_run.undo) - # Any sys.module or sys.path changes done while running pytest # inline should be reverted after the test run completes to avoid # clashing with later inline tests run within the same pytest test, @@ -1091,7 +1086,6 @@ class Testdir: env["PYTHONPATH"] = os.pathsep.join( filter(None, [os.getcwd(), env.get("PYTHONPATH", "")]) ) - env.update(self._env_run_update) kw["env"] = env if stdin is Testdir.CLOSE_STDIN: @@ -1261,11 +1255,7 @@ class Testdir: pytest.skip("pexpect.spawn not available") logfile = self.tmpdir.join("spawn.out").open("wb") - # Do not load user config. - env = os.environ.copy() - env.update(self._env_run_update) - - child = pexpect.spawn(cmd, logfile=logfile, env=env) + child = pexpect.spawn(cmd, logfile=logfile) self.request.addfinalizer(logfile.close) child.timeout = expect_timeout return child diff --git a/testing/test_pdb.py b/testing/test_pdb.py index 25d2292e9..0f7816dbe 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -22,7 +22,7 @@ def pdb_env(request): if "testdir" in request.fixturenames: # Disable pdb++ with inner tests. testdir = request.getfixturevalue("testdir") - testdir._env_run_update["PDBPP_HIJACK_PDB"] = "0" + testdir.monkeypatch.setenv("PDBPP_HIJACK_PDB", "0") def runpdb_and_get_report(testdir, source): diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 5bdbacdd0..3dab13b4b 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -550,17 +550,15 @@ def test_no_matching_after_match(): assert str(e.value).splitlines() == ["fnmatch: '*'", " with: '1'"] -def test_pytester_addopts(request, monkeypatch): +def test_pytester_addopts_before_testdir(request, monkeypatch): + orig = os.environ.get("PYTEST_ADDOPTS", None) monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused") - testdir = request.getfixturevalue("testdir") - - try: - assert "PYTEST_ADDOPTS" not in os.environ - finally: - testdir.finalize() - - assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused" + assert "PYTEST_ADDOPTS" not in os.environ + testdir.finalize() + assert os.environ.get("PYTEST_ADDOPTS") == "--orig-unused" + monkeypatch.undo() + assert os.environ.get("PYTEST_ADDOPTS") == orig def test_run_stdin(testdir): @@ -640,14 +638,10 @@ def test_popen_default_stdin_stderr_and_stdin_None(testdir): def test_spawn_uses_tmphome(testdir): - import os - tmphome = str(testdir.tmpdir) + assert os.environ.get("HOME") == tmphome - # Does use HOME only during run. - assert os.environ.get("HOME") != tmphome - - testdir._env_run_update["CUSTOMENV"] = "42" + testdir.monkeypatch.setenv("CUSTOMENV", "42") p1 = testdir.makepyfile( """