pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.87k stars 2.65k forks source link

access `config` object in hook `pytest_runtest_logstart` and `pytest_runtest_logfinish` #6581

Closed toolsh closed 4 years ago

toolsh commented 4 years ago

Background

Issue

def pytest_runtest_protocol(item, nextitem):
    item.ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location)
    runtestprotocol(item, nextitem=nextitem)
    item.ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location)
    return True
nicoddemus commented 4 years ago

@toolsh,

A common way to workaround this is by installing a "subplugin":

# conftest.py
def pytest_configure(config):
    config.pluginmanager.register(MyPlugin())

class MyPlugin:

    def pytest_configure(self, config):
        self.config = config

    def pytest_runtest_logstart(self, nodeid, location):
        ...  # access self.config

    def pytest_runtest_logfinish(self, nodeid, location):
        ...  # access self.config

(Similar approach to access session).

Adding new parameters to hooks is usually trivial, but unfortunately adding the config parameter to those two hooks in particular are a bit problematic because we need to also update pytest-xdist, which calls the hooks directly.

Zac-HD commented 4 years ago

Closing this as the question seems to have been answered 😄