Skip to content

Commit

Permalink
pytester: use monkeypatch fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Nov 18, 2019
1 parent a2d4833 commit ba5b75c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog/6213.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytester: the ``testdir`` fixture uses the ``monkeypatch`` fixture for changing the environment.
16 changes: 10 additions & 6 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ def LineMatcher_fixture(request: FixtureRequest) -> "Type[LineMatcher]":


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


@pytest.fixture
Expand Down Expand Up @@ -524,21 +524,25 @@ class Testdir:
class TimeoutExpired(Exception):
pass

def __init__(self, request, tmpdir_factory):
def __init__(
self, request, tmpdir_factory, *, monkeypatch: Optional[MonkeyPatch] = None
) -> None:
self.request = request
self._mod_collections = WeakKeyDictionary()
self._mod_collections = WeakKeyDictionary() # type: ignore
name = request.function.__name__
self.tmpdir = tmpdir_factory.mktemp(name, numbered=True)
self.test_tmproot = tmpdir_factory.mktemp("tmp-" + name, numbered=True)
self.plugins = []
self.plugins = [] # type: ignore
self._cwd_snapshot = CwdSnapshot()
self._sys_path_snapshot = SysPathsSnapshot()
self._sys_modules_snapshot = self.__take_sys_modules_snapshot()
self.chdir()
self.request.addfinalizer(self.finalize)
self._method = self.request.config.getoption("--runpytest")

mp = self.monkeypatch = MonkeyPatch()
if monkeypatch is None:
monkeypatch = MonkeyPatch()
mp = self.monkeypatch = monkeypatch
mp.setenv("PYTEST_DEBUG_TEMPROOT", str(self.test_tmproot))
# Ensure no unexpected caching via tox.
mp.delenv("TOX_ENV_DIR", raising=False)
Expand Down
27 changes: 18 additions & 9 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,26 @@ def test_no_matching(function):
func(bad_pattern) # bad pattern does not match any line: passes


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


@pytest.mark.parametrize("method", ("setenv", "delenv"))
def test_testdir_respects_monkeypatch(method, testdir, monkeypatch):
assert monkeypatch is testdir.monkeypatch
assert testdir._env_run_update["COLUMNS"] == "80"
assert testdir._get_env_run_update()["COLUMNS"] == "80"
if method == "setenv":
monkeypatch.setenv("COLUMNS", "12")
else:
assert method == "delenv"
monkeypatch.delenv("COLUMNS", raising=False)
assert "COLUMNS" not in testdir._get_env_run_update()


def test_run_stdin(testdir):
Expand Down

0 comments on commit ba5b75c

Please sign in to comment.