Mention `monkeypatch.context()` in the docs (#10192)
In addition: * Improve the docs a bit with links. * Recommend `context()` instead of `undo()`.
This commit is contained in:
parent
c72d202317
commit
739322af03
|
@ -29,21 +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. The ``raising`` parameter determines if a KeyError
|
fixture has finished. The ``raising`` parameter determines if a :class:`KeyError`
|
||||||
or AttributeError will be raised if the set/deletion operation has no target.
|
or :class:`AttributeError` will be raised if the set/deletion operation does not have the
|
||||||
|
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
|
||||||
|
@ -353,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:
|
||||||
|
|
Loading…
Reference in New Issue