add hooks before and after a module is imported

This commit is contained in:
Benjamin Peterson 2011-05-19 21:45:33 -05:00
parent 78be3db9bb
commit bf039fea74
2 changed files with 9 additions and 0 deletions

View File

@ -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

View File

@ -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