[TIP] pytest: How to implement a new hook that only operates on subdirectories?

Dan Wandschneider daniel.wandschneider at schrodinger.com
Tue Nov 15 16:57:59 PST 2016


I'd like to implement a pytest hook that allows test directories to have an
"owner". All subdirs of a test directory should be owned by the same
person, unless that directory is itself owned.

I thought that this would work:

class newhooks:
     @staticmethod
     @hookspec(firstresult=True)
     def pytest_test_owner():
         """
         Specify the owner of tests in this directory.
         @return: a username
         """
         pass
pluginmanager.add_hookspecs(newhooks)

then use like:

def pytest_runtest_makereport(item, call):
    """Add the test owner to each report."""
    report = _pytest.runner.pytest_runtest_makereport(item, call)
    if not report.passed:
        owner = item.ihook.pytest_test_owner()
        if owner:
            report.sections.append(('owner: ' + owner))

However, this *actually* sets the owner to whichever conftest was imported
last. The pytest hook docs say "Session and test running activities will
invoke all hooks defined in conftest.py files closer to the root of the
filesystem." (pytest docs
<http://pytest.org/2.2.4/plugins.html#conftest-py-local-per-directory-plugins>)
How can I get similar behavior for my new hook?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161115/ea3c00b7/attachment.htm>


More information about the testing-in-python mailing list