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

Method hook

testing/test_assertrewrite.py:1380–1407  ·  view source on GitHub ↗

Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track if PathFinder.find_spec has been called.

(
        self, pytestconfig, monkeypatch, pytester: Pytester
    )

Source from the content-addressed store, hash-verified

1378class TestEarlyRewriteBailout:
1379 @pytest.fixture
1380 def hook(
1381 self, pytestconfig, monkeypatch, pytester: Pytester
1382 ) -> AssertionRewritingHook:
1383 """Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
1384 if PathFinder.find_spec has been called.
1385 """
1386 import importlib.machinery
1387
1388 self.find_spec_calls: List[str] = []
1389 self.initial_paths: Set[Path] = set()
1390
1391 class StubSession:
1392 _initialpaths = self.initial_paths
1393
1394 def isinitpath(self, p):
1395 return p in self._initialpaths
1396
1397 def spy_find_spec(name, path):
1398 self.find_spec_calls.append(name)
1399 return importlib.machinery.PathFinder.find_spec(name, path)
1400
1401 hook = AssertionRewritingHook(pytestconfig)
1402 # use default patterns, otherwise we inherit pytest's testing config
1403 hook.fnpats[:] = ["test_*.py", "*_test.py"]
1404 monkeypatch.setattr(hook, "_find_spec", spy_find_spec)
1405 hook.set_session(StubSession()) # type: ignore[arg-type]
1406 pytester.syspathinsert()
1407 return hook
1408
1409 def test_basic(self, pytester: Pytester, hook: AssertionRewritingHook) -> None:
1410 """

Callers

nothing calls this directly

Calls 5

set_sessionMethod · 0.95
StubSessionClass · 0.85
setattrMethod · 0.80
syspathinsertMethod · 0.45

Tested by

no test coverage detected