def hook(self, pytestconfig, monkeypatch, testdir): """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 = [] self.initial_paths = 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 hook.fnpats[:] = ["test_*.py", "*_test.py"] monkeypatch.setattr(hook, "_find_spec", spy_find_spec) hook.set_session(StubSession()) testdir.syspathinsert() return hook
def hook(self, pytestconfig, monkeypatch, testdir): """Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track if imp.find_module has been called. """ import imp self.find_module_calls = [] self.initial_paths = set() class StubSession(object): _initialpaths = self.initial_paths def isinitpath(self, p): return p in self._initialpaths def spy_imp_find_module(name, path): self.find_module_calls.append(name) return imp.find_module(name, path) hook = AssertionRewritingHook(pytestconfig) # use default patterns, otherwise we inherit pytest's testing config hook.fnpats[:] = ["test_*.py", "*_test.py"] monkeypatch.setattr(hook, "_imp_find_module", spy_imp_find_module) hook.set_session(StubSession()) testdir.syspathinsert() return hook