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

Method context

src/_pytest/monkeypatch.py:135–158  ·  view source on GitHub ↗

Context manager that returns a new :class:`MonkeyPatch` object which undoes any patching done inside the ``with`` block upon exit. Example: .. code-block:: python import functools def test_partial(monkeypatch): with monkeypatch.con

(cls)

Source from the content-addressed store, hash-verified

133 @classmethod
134 @contextmanager
135 def context(cls) -> Generator[MonkeyPatch]:
136 """Context manager that returns a new :class:`MonkeyPatch` object
137 which undoes any patching done inside the ``with`` block upon exit.
138
139 Example:
140
141 .. code-block:: python
142
143 import functools
144
145
146 def test_partial(monkeypatch):
147 with monkeypatch.context() as m:
148 m.setattr(functools, "partial", 3)
149
150 Useful in situations where it is desired to undo some patches before the test ends,
151 such as mocking ``stdlib`` functions that might break pytest itself if mocked (for examples
152 of this see :issue:`3290`).
153 """
154 m = cls()
155 try:
156 yield m
157 finally:
158 m.undo()
159
160 @overload
161 def setattr(

Calls 2

clsFunction · 0.85
undoMethod · 0.80