Skip to content

Commit 13fd967

Browse files
authored
Merge pull request #10213 from pytest-dev/backport-10192-to-7.1.x
2 parents d7f27c4 + f5cdd18 commit 13fd967

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/_pytest/monkeypatch.py

+25-18
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@
2929
def monkeypatch() -> Generator["MonkeyPatch", None, None]:
3030
"""A convenient fixture for monkey-patching.
3131
32-
The fixture provides these methods to modify objects, dictionaries or
33-
os.environ::
34-
35-
monkeypatch.setattr(obj, name, value, raising=True)
36-
monkeypatch.delattr(obj, name, raising=True)
37-
monkeypatch.setitem(mapping, name, value)
38-
monkeypatch.delitem(obj, name, raising=True)
39-
monkeypatch.setenv(name, value, prepend=None)
40-
monkeypatch.delenv(name, raising=True)
41-
monkeypatch.syspath_prepend(path)
42-
monkeypatch.chdir(path)
32+
The fixture provides these methods to modify objects, dictionaries, or
33+
:data:`os.environ`:
34+
35+
* :meth:`monkeypatch.setattr(obj, name, value, raising=True) <pytest.MonkeyPatch.setattr>`
36+
* :meth:`monkeypatch.delattr(obj, name, raising=True) <pytest.MonkeyPatch.delattr>`
37+
* :meth:`monkeypatch.setitem(mapping, name, value) <pytest.MonkeyPatch.setitem>`
38+
* :meth:`monkeypatch.delitem(obj, name, raising=True) <pytest.MonkeyPatch.delitem>`
39+
* :meth:`monkeypatch.setenv(name, value, prepend=None) <pytest.MonkeyPatch.setenv>`
40+
* :meth:`monkeypatch.delenv(name, raising=True) <pytest.MonkeyPatch.delenv>`
41+
* :meth:`monkeypatch.syspath_prepend(path) <pytest.MonkeyPatch.syspath_prepend>`
42+
* :meth:`monkeypatch.chdir(path) <pytest.MonkeyPatch.chdir>`
4343
4444
All modifications will be undone after the requesting test function or
45-
fixture has finished. The ``raising`` parameter determines if a KeyError
46-
or AttributeError will be raised if the set/deletion operation has no target.
45+
fixture has finished. The ``raising`` parameter determines if a :class:`KeyError`
46+
or :class:`AttributeError` will be raised if the set/deletion operation does not have the
47+
specified target.
48+
49+
To undo modifications done by the fixture in a contained scope,
50+
use :meth:`context() <pytest.MonkeyPatch.context>`.
4751
"""
4852
mpatch = MonkeyPatch()
4953
yield mpatch
@@ -353,11 +357,14 @@ def undo(self) -> None:
353357
There is generally no need to call `undo()`, since it is
354358
called automatically during tear-down.
355359
356-
Note that the same `monkeypatch` fixture is used across a
357-
single test function invocation. If `monkeypatch` is used both by
358-
the test function itself and one of the test fixtures,
359-
calling `undo()` will undo all of the changes made in
360-
both functions.
360+
.. note::
361+
The same `monkeypatch` fixture is used across a
362+
single test function invocation. If `monkeypatch` is used both by
363+
the test function itself and one of the test fixtures,
364+
calling `undo()` will undo all of the changes made in
365+
both functions.
366+
367+
Prefer to use :meth:`context() <pytest.MonkeyPatch.context>` instead.
361368
"""
362369
for obj, name, value in reversed(self._setattr):
363370
if value is not notset:

0 commit comments

Comments
 (0)