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

Method hook

testing/test_assertrewrite.py:1912–1939  ·  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

1910class TestEarlyRewriteBailout:
1911 @pytest.fixture
1912 def hook(
1913 self, pytestconfig, monkeypatch, pytester: Pytester
1914 ) -> Generator[AssertionRewritingHook]:
1915 """Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
1916 if PathFinder.find_spec has been called.
1917 """
1918 import importlib.machinery
1919
1920 self.find_spec_calls: list[str] = []
1921 self.initial_paths: set[Path] = set()
1922
1923 class StubSession:
1924 _initialpaths = self.initial_paths
1925
1926 def isinitpath(self, p):
1927 return p in self._initialpaths
1928
1929 def spy_find_spec(name, path):
1930 self.find_spec_calls.append(name)
1931 return importlib.machinery.PathFinder.find_spec(name, path)
1932
1933 hook = AssertionRewritingHook(pytestconfig)
1934 # use default patterns, otherwise we inherit pytest's testing config
1935 with mock.patch.object(hook, "fnpats", ["test_*.py", "*_test.py"]):
1936 monkeypatch.setattr(hook, "_find_spec", spy_find_spec)
1937 hook.set_session(StubSession()) # type: ignore[arg-type]
1938 pytester.syspathinsert()
1939 yield hook
1940
1941 def test_basic(self, pytester: Pytester, hook: AssertionRewritingHook) -> None:
1942 """

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