From 962d0fe2bead71e66c05bf8acb829ed26d158385 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 29 Apr 2010 16:53:29 +0200 Subject: [PATCH] introduce new pytest_pycollect_makemodule(path, parent) hook for allowing customization of the Module collection object for a matching test module. --HG-- branch : trunk --- CHANGELOG | 3 +++ py/_plugin/hookspec.py | 8 ++++++++ py/_plugin/pytest_default.py | 6 +++++- testing/test_pycollect.py | 17 +++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 229ae5bdb..c442028d8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/py/_plugin/hookspec.py b/py/_plugin/hookspec.py index a1d7ef7de..8d8ae18b0 100644 --- a/py/_plugin/hookspec.py +++ b/py/_plugin/hookspec.py @@ -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 diff --git a/py/_plugin/pytest_default.py b/py/_plugin/pytest_default.py index 93ddbf37b..b5142ef86 100644 --- a/py/_plugin/pytest_default.py +++ b/py/_plugin/pytest_default.py @@ -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.""" diff --git a/testing/test_pycollect.py b/testing/test_pycollect.py index 416bacbcb..af70b2b66 100644 --- a/testing/test_pycollect.py +++ b/testing/test_pycollect.py @@ -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([ + "*