首页 > 解决方案 > 访问 runtest 挂钩内的 pytest PluginManager 对象

问题描述

在 pytest 插件中,我想访问runtest钩子内的配置。这行得通,但感觉就像一个黑客。有更清洁的方法吗?

configuration = None


def pytest_configure(config):
    global configuration
    configuration = config


def pytest_runtest_call(item):
    manager = configuration.pluginmanager
    # Do something with `manager` here.
    ...

标签: pythonpytest

解决方案


您始终可以通过会话对象访问配置:

def pytest_runtest_call(item):
    manager = item.session.config.pluginmanager 

推荐阅读