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

Class HookRecorder

src/_pytest/pytester.py:245–459  ·  view source on GitHub ↗

Record all hooks called in a plugin manager. Hook recorders are created by :class:`Pytester`. This wraps all the hook calls in the plugin manager, recording each call before propagating the normal calls.

Source from the content-addressed store, hash-verified

243
244@final
245class HookRecorder:
246 """Record all hooks called in a plugin manager.
247
248 Hook recorders are created by :class:`Pytester`.
249
250 This wraps all the hook calls in the plugin manager, recording each call
251 before propagating the normal calls.
252 """
253
254 def __init__(
255 self, pluginmanager: PytestPluginManager, *, _ispytest: bool = False
256 ) -> None:
257 check_ispytest(_ispytest)
258
259 self._pluginmanager = pluginmanager
260 self.calls: List[RecordedHookCall] = []
261 self.ret: Optional[Union[int, ExitCode]] = None
262
263 def before(hook_name: str, hook_impls, kwargs) -> None:
264 self.calls.append(RecordedHookCall(hook_name, kwargs))
265
266 def after(outcome, hook_name: str, hook_impls, kwargs) -> None:
267 pass
268
269 self._undo_wrapping = pluginmanager.add_hookcall_monitoring(before, after)
270
271 def finish_recording(self) -> None:
272 self._undo_wrapping()
273
274 def getcalls(self, names: Union[str, Iterable[str]]) -> List[RecordedHookCall]:
275 """Get all recorded calls to hooks with the given names (or name)."""
276 if isinstance(names, str):
277 names = names.split()
278 return [call for call in self.calls if call._name in names]
279
280 def assert_contains(self, entries: Sequence[Tuple[str, str]]) -> None:
281 __tracebackhide__ = True
282 i = 0
283 entries = list(entries)
284 backlocals = sys._getframe(1).f_locals
285 while entries:
286 name, check = entries.pop(0)
287 for ind, call in enumerate(self.calls[i:]):
288 if call._name == name:
289 print("NAMEMATCH", name, call)
290 if eval(check, backlocals, call.__dict__):
291 print("CHECKERMATCH", repr(check), "->", call)
292 else:
293 print("NOCHECKERMATCH", repr(check), "-", call)
294 continue
295 i += ind + 1
296 break
297 print("NONAMEMATCH", name, "with", call)
298 else:
299 fail(f"could not find {name!r} check {check!r}")
300
301 def popcall(self, name: str) -> RecordedHookCall:
302 __tracebackhide__ = True

Callers 3

test_hookrecorder_basicFunction · 0.90
gethookrecorderMethod · 0.85
make_hook_recorderMethod · 0.85

Calls

no outgoing calls

Tested by 3

test_hookrecorder_basicFunction · 0.72
gethookrecorderMethod · 0.68
make_hook_recorderMethod · 0.68