MCPcopy
hub / github.com/pytest-dev/pytest / monkeypatch

Function monkeypatch

src/_pytest/monkeypatch.py:36–62  ·  view source on GitHub ↗

A convenient fixture for monkey-patching. The fixture provides these methods to modify objects, dictionaries, or :data:`os.environ`: * :meth:`monkeypatch.setattr(obj, name, value, raising=True) <pytest.MonkeyPatch.setattr>` * :meth:`monkeypatch.delattr(obj, name, raising=True) <pyt

()

Source from the content-addressed store, hash-verified

34
35@fixture
36def monkeypatch() -> Generator[MonkeyPatch]:
37 """A convenient fixture for monkey-patching.
38
39 The fixture provides these methods to modify objects, dictionaries, or
40 :data:`os.environ`:
41
42 * :meth:`monkeypatch.setattr(obj, name, value, raising=True) <pytest.MonkeyPatch.setattr>`
43 * :meth:`monkeypatch.delattr(obj, name, raising=True) <pytest.MonkeyPatch.delattr>`
44 * :meth:`monkeypatch.setitem(mapping, name, value) <pytest.MonkeyPatch.setitem>`
45 * :meth:`monkeypatch.delitem(obj, name, raising=True) <pytest.MonkeyPatch.delitem>`
46 * :meth:`monkeypatch.setenv(name, value, prepend=None) <pytest.MonkeyPatch.setenv>`
47 * :meth:`monkeypatch.delenv(name, raising=True) <pytest.MonkeyPatch.delenv>`
48 * :meth:`monkeypatch.syspath_prepend(path) <pytest.MonkeyPatch.syspath_prepend>`
49 * :meth:`monkeypatch.chdir(path) <pytest.MonkeyPatch.chdir>`
50 * :meth:`monkeypatch.context() <pytest.MonkeyPatch.context>`
51
52 All modifications will be undone after the requesting test function or
53 fixture has finished. The ``raising`` parameter determines if a :class:`KeyError`
54 or :class:`AttributeError` will be raised if the set/deletion operation does not have the
55 specified target.
56
57 To undo modifications done by the fixture in a contained scope,
58 use :meth:`context() <pytest.MonkeyPatch.context>`.
59 """
60 mpatch = MonkeyPatch()
61 yield mpatch
62 mpatch.undo()
63
64
65def resolve(name: str) -> object:

Callers

nothing calls this directly

Calls 2

undoMethod · 0.95
MonkeyPatchClass · 0.85

Tested by

no test coverage detected