From bf039fea74a3d1657a849863fa67385bae3bb58b Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 19 May 2011 21:45:33 -0500 Subject: [PATCH] add hooks before and after a module is imported --- _pytest/hookspec.py | 6 ++++++ _pytest/python.py | 3 +++ 2 files changed, 9 insertions(+) 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