def hook(
        self, pytestconfig, monkeypatch, pytester: Pytester
    ) -> Generator[AssertionRewritingHook]:
        """Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
        if PathFinder.find_spec has been called.
        """
        import importlib.machinery

        self.find_spec_calls: list[str] = []
        self.initial_paths: set[Path] = set()

        class StubSession:
            _initialpaths = self.initial_paths

            def isinitpath(self, p):
                return p in self._initialpaths

        def spy_find_spec(name, path):
            self.find_spec_calls.append(name)
            return importlib.machinery.PathFinder.find_spec(name, path)

        hook = AssertionRewritingHook(pytestconfig)
        # use default patterns, otherwise we inherit pytest's testing config
        with mock.patch.object(hook, "fnpats", ["test_*.py", "*_test.py"]):
            monkeypatch.setattr(hook, "_find_spec", spy_find_spec)
            hook.set_session(StubSession())  # type: ignore[arg-type]
            pytester.syspathinsert()
            yield hook
Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
        if PathFinder.find_spec has been called.
        
testing/test_assertrewrite.py