Recommend using context() instead of mentioning undo()

Also use more links in the docs.
This commit is contained in:
Bruno Oliveira 2022-08-12 08:57:14 -03:00
parent 1ffcb9224f
commit 5d20f70ba1
1 changed files with 24 additions and 18 deletions

View File

@ -29,22 +29,25 @@ V = TypeVar("V")
def monkeypatch() -> Generator["MonkeyPatch", None, None]: def monkeypatch() -> Generator["MonkeyPatch", None, None]:
"""A convenient fixture for monkey-patching. """A convenient fixture for monkey-patching.
The fixture provides these methods to modify objects, dictionaries or The fixture provides these methods to modify objects, dictionaries, or
os.environ:: :data:`os.environ`:
monkeypatch.setattr(obj, name, value, raising=True) * :meth:`monkeypatch.setattr(obj, name, value, raising=True) <pytest.MonkeyPatch.setattr>`
monkeypatch.delattr(obj, name, raising=True) * :meth:`monkeypatch.delattr(obj, name, raising=True) <pytest.MonkeyPatch.delattr>`
monkeypatch.setitem(mapping, name, value) * :meth:`monkeypatch.setitem(mapping, name, value) <pytest.MonkeyPatch.setitem>`
monkeypatch.delitem(obj, name, raising=True) * :meth:`monkeypatch.delitem(obj, name, raising=True) <pytest.MonkeyPatch.delitem>`
monkeypatch.setenv(name, value, prepend=None) * :meth:`monkeypatch.setenv(name, value, prepend=None) <pytest.MonkeyPatch.setenv>`
monkeypatch.delenv(name, raising=True) * :meth:`monkeypatch.delenv(name, raising=True) <pytest.MonkeyPatch.delenv>`
monkeypatch.syspath_prepend(path) * :meth:`monkeypatch.syspath_prepend(path) <pytest.MonkeyPatch.syspath_prepend>`
monkeypatch.chdir(path) * :meth:`monkeypatch.chdir(path) <pytest.MonkeyPatch.chdir>`
All modifications will be undone after the requesting test function or All modifications will be undone after the requesting test function or
fixture has finished. It is possible to undo them earlier by calling fixture has finished. The ``raising`` parameter determines if a :class:`KeyError`
``monkeypatch.undo()``. The ``raising`` parameter determines if a KeyError or :class:`AttributeError` will be raised if the set/deletion operation does not have the
or AttributeError will be raised if the set/deletion operation has no target. specified target.
To undo modifications done by the fixture in a contained scope,
use :meth:`context() <pytest.MonkeyPatch.context>`.
""" """
mpatch = MonkeyPatch() mpatch = MonkeyPatch()
yield mpatch yield mpatch
@ -354,11 +357,14 @@ class MonkeyPatch:
There is generally no need to call `undo()`, since it is There is generally no need to call `undo()`, since it is
called automatically during tear-down. called automatically during tear-down.
Note that the same `monkeypatch` fixture is used across a .. note::
single test function invocation. If `monkeypatch` is used both by The same `monkeypatch` fixture is used across a
the test function itself and one of the test fixtures, single test function invocation. If `monkeypatch` is used both by
calling `undo()` will undo all of the changes made in the test function itself and one of the test fixtures,
both functions. calling `undo()` will undo all of the changes made in
both functions.
Prefer to use :meth:`context() <pytest.MonkeyPatch.context>` instead.
""" """
for obj, name, value in reversed(self._setattr): for obj, name, value in reversed(self._setattr):
if value is not notset: if value is not notset: