introduce new pytest_pycollect_makemodule(path, parent) hook for

allowing customization of the Module collection object for a matching test module.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-04-29 16:53:29 +02:00
parent 811408959f
commit 962d0fe2be
4 changed files with 33 additions and 1 deletions

View File

@ -14,6 +14,9 @@ Changes between 1.2.1 and 1.2.2 (release pending)
def pytest_ignore_collect_path(path):
return path.check(link=1)
to prevent even a collection try of any tests in symlinked dirs.
- introduce new pytest_pycollect_makemodule(path, parent) hook for
allowing customization of the Module collection object for a
matching test module.
- expose previously internal commonly useful methods:
py.io.get_terminal_with() -> return terminal width
py.io.ansi_print(...) -> print colored/bold text on linux/win32

View File

@ -62,6 +62,14 @@ def pytest_itemstart(item, node=None):
# Python test function related hooks
# -------------------------------------------------------------------------
def pytest_pycollect_makemodule(path, parent):
""" return a Module collector or None for the given path.
This hook will be called for each matching test module path.
The pytest_collect_file hook needs to be used if you want to
create test modules for files that do not match as a test module.
"""
pytest_pycollect_makemodule.firstresult = True
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

@ -18,7 +18,11 @@ def pytest_collect_file(path, parent):
if pb.startswith("test_") or pb.endswith("_test") or \
path in parent.config._argfspaths:
if ext == ".py":
return parent.Module(path, parent=parent)
return parent.ihook.pytest_pycollect_makemodule(
path=path, parent=parent)
def pytest_pycollect_makemodule(path, parent):
return parent.Module(path, parent)
def pytest_funcarg__pytestconfig(request):
""" the pytest config object with access to command line opts."""

View File

@ -313,6 +313,23 @@ class TestSorting:
class TestConftestCustomization:
def test_pytest_pycollect_module(self, testdir):
testdir.makeconftest("""
import py
class MyModule(py.test.collect.Module):
pass
def pytest_pycollect_makemodule(path, parent):
if path.basename == "test_xyz.py":
return MyModule(path, parent)
""")
testdir.makepyfile("def some(): pass")
testdir.makepyfile(test_xyz="")
result = testdir.runpytest("--collectonly")
result.stdout.fnmatch_lines([
"*<Module*test_pytest*",
"*<MyModule*xyz*",
])
def test_pytest_pycollect_makeitem(self, testdir):
testdir.makeconftest("""
import py