Skip to content

Commit d47d808

Browse files
committed
pytester: use monkeypatch fixture
Fixes #6213.
1 parent a2d4833 commit d47d808

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

changelog/6213.improvement.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytester: the ``testdir`` fixture uses the ``monkeypatch`` fixture for changing the environment.

src/_pytest/pytester.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ def LineMatcher_fixture(request: FixtureRequest) -> "Type[LineMatcher]":
359359

360360

361361
@pytest.fixture
362-
def testdir(request: FixtureRequest, tmpdir_factory) -> "Testdir":
363-
return Testdir(request, tmpdir_factory)
362+
def testdir(request: FixtureRequest, tmpdir_factory, monkeypatch) -> "Testdir":
363+
return Testdir(request, tmpdir_factory, monkeypatch=monkeypatch)
364364

365365

366366
@pytest.fixture
@@ -524,21 +524,26 @@ class Testdir:
524524
class TimeoutExpired(Exception):
525525
pass
526526

527-
def __init__(self, request, tmpdir_factory):
527+
def __init__(
528+
self, request, tmpdir_factory, *, monkeypatch: Optional[MonkeyPatch] = None
529+
) -> None:
528530
self.request = request
529-
self._mod_collections = WeakKeyDictionary()
531+
self._mod_collections = WeakKeyDictionary() # type: ignore
530532
name = request.function.__name__
531533
self.tmpdir = tmpdir_factory.mktemp(name, numbered=True)
532534
self.test_tmproot = tmpdir_factory.mktemp("tmp-" + name, numbered=True)
533-
self.plugins = []
535+
self.plugins = [] # type: ignore
534536
self._cwd_snapshot = CwdSnapshot()
535537
self._sys_path_snapshot = SysPathsSnapshot()
536538
self._sys_modules_snapshot = self.__take_sys_modules_snapshot()
537539
self.chdir()
538540
self.request.addfinalizer(self.finalize)
539541
self._method = self.request.config.getoption("--runpytest")
540542

541-
mp = self.monkeypatch = MonkeyPatch()
543+
if monkeypatch is None:
544+
mp = self.monkeypatch = self.request.getfixturevalue("monkeypatch")
545+
else:
546+
mp = self.monkeypatch = monkeypatch
542547
mp.setenv("PYTEST_DEBUG_TEMPROOT", str(self.test_tmproot))
543548
# Ensure no unexpected caching via tox.
544549
mp.delenv("TOX_ENV_DIR", raising=False)

testing/test_pytester.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -542,17 +542,26 @@ def test_no_matching(function):
542542
func(bad_pattern) # bad pattern does not match any line: passes
543543

544544

545-
def test_pytester_addopts(request, monkeypatch):
545+
def test_pytester_addopts_before_testdir(request, monkeypatch):
546+
orig = os.environ.get("PYTEST_ADDOPTS", None)
546547
monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused")
547-
548548
testdir = request.getfixturevalue("testdir")
549-
550-
try:
551-
assert "PYTEST_ADDOPTS" not in os.environ
552-
finally:
553-
testdir.finalize()
554-
555-
assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused"
549+
assert "PYTEST_ADDOPTS" not in os.environ
550+
testdir.finalize()
551+
assert os.environ.get("PYTEST_ADDOPTS") == orig
552+
553+
554+
@pytest.mark.parametrize("method", ("setenv", "delenv"))
555+
def test_testdir_respects_monkeypatch(method, testdir, monkeypatch):
556+
assert monkeypatch is testdir.monkeypatch
557+
assert testdir._env_run_update["COLUMNS"] == "80"
558+
assert testdir._get_env_run_update()["COLUMNS"] == "80"
559+
if method == "setenv":
560+
monkeypatch.setenv("COLUMNS", "12")
561+
else:
562+
assert method == "delenv"
563+
monkeypatch.delenv("COLUMNS", raising=False)
564+
assert "COLUMNS" not in testdir._get_env_run_update()
556565

557566

558567
def test_run_stdin(testdir):

0 commit comments

Comments
 (0)