|
29 | 29 | def monkeypatch() -> Generator["MonkeyPatch", None, None]:
|
30 | 30 | """A convenient fixture for monkey-patching.
|
31 | 31 |
|
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>` |
43 | 43 |
|
44 | 44 | 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>`. |
47 | 51 | """
|
48 | 52 | mpatch = MonkeyPatch()
|
49 | 53 | yield mpatch
|
@@ -353,11 +357,14 @@ def undo(self) -> None:
|
353 | 357 | There is generally no need to call `undo()`, since it is
|
354 | 358 | called automatically during tear-down.
|
355 | 359 |
|
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. |
361 | 368 | """
|
362 | 369 | for obj, name, value in reversed(self._setattr):
|
363 | 370 | if value is not notset:
|
|
0 commit comments