MCPcopy Create free account
hub / github.com/pytest-dev/pytest / monkeypatch

Function monkeypatch

src/_pytest/monkeypatch.py:29–50  ·  view source on GitHub ↗

A convenient fixture for monkey-patching. The fixture provides these methods to modify objects, dictionaries or os.environ:: monkeypatch.setattr(obj, name, value, raising=True) monkeypatch.delattr(obj, name, raising=True) monkeypatch.setitem(mapping, name, value)

()

Source from the content-addressed store, hash-verified

27
28@fixture
29def monkeypatch() -> Generator["MonkeyPatch", None, None]:
30 """A convenient fixture for monkey-patching.
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)
43
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.
47 """
48 mpatch = MonkeyPatch()
49 yield mpatch
50 mpatch.undo()
51
52
53def resolve(name: str) -> object:

Callers

nothing calls this directly

Calls 2

undoMethod · 0.95
MonkeyPatchClass · 0.85

Tested by

no test coverage detected