diff --git a/_pytest/hookspec.py b/_pytest/hookspec.py index 898ffee2a..7cb60a131 100644 --- a/_pytest/hookspec.py +++ b/_pytest/hookspec.py @@ -104,6 +104,12 @@ def pytest_pycollect_makemodule(path, parent): """ pytest_pycollect_makemodule.firstresult = True +def pytest_pycollect_before_module_import(mod): + """Called before a module is imported.""" + +def pytest_pycollect_after_module_import(mod): + """Called after a module is imported.""" + def pytest_pycollect_makeitem(collector, name, obj): """ return custom item/collector for a python object in a module, or None. """ pytest_pycollect_makeitem.firstresult = True diff --git a/_pytest/python.py b/_pytest/python.py index f05aa1447..e2aa5a754 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -225,6 +225,7 @@ class Module(pytest.File, PyCollectorMixin): return self._memoizedcall('_obj', self._importtestmodule) def _importtestmodule(self): + self.ihook.pytest_pycollect_before_module_import(mod=self) # we assume we are only called once per module try: mod = self.fspath.pyimport(ensuresyspath=True) @@ -242,6 +243,8 @@ class Module(pytest.File, PyCollectorMixin): "HINT: use a unique basename for your test file modules" % e.args ) + finally: + self.ihook.pytest_pycollect_after_module_import(mod=self) #print "imported test module", mod self.config.pluginmanager.consider_module(mod) return mod