Skip to content

terminalreporter plugin not available in pytest_configure hook #939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mblayman opened this issue Aug 14, 2015 · 3 comments
Closed

terminalreporter plugin not available in pytest_configure hook #939

mblayman opened this issue Aug 14, 2015 · 3 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@mblayman
Copy link

The documentation indicates that plugins should be able to interact with other plugins via config.pluginmanager.getplugin(name).

The pytest_configure hook documentation reads (emphasis added):

called after command line options have been parsed and all plugins and initial conftest files been loaded.

And the order for plugin discovery is supposed to be built-ins, setuptools plugins, command line declared plugins, and conftest plugins.

In spite of that, I am unable to get the terminalreporter plugin in my pytest_configure hook.

Here is a basic conftest.py plugin to demonstrate the problem:

def pytest_configure(config):                                                    
    assert config.pluginmanager.hasplugin('terminalreporter')

My use case is that I would like to unregister the terminal reporter and display only output from my plugin (I'm trying to stream Test Anything Protocol output directly from py.test). Unless I disable terminalreporter from the command line with -p no:terminalreporter, I am not able to suppress terminal reporter output.

I see that the terminal reporter isn't registered until terminal.py has its pytest_configure hook invoked, but since terminal.py is a built-in, I don't understand why the plugin is not available downstream in the conftest.py plugin.

I am running pytest 2.7.2 with Python 2.7.6 on Ubuntu 14.04. Thanks for reading this far! 😄

@nicoddemus
Copy link
Member

Hi,

All plugins are in fact loaded by the time pytest_configure is called. This for example works:

def pytest_configure(config):                                                    
    assert config.pluginmanager.hasplugin('terminal')

(Note that says terminal as opposed to terminalreporter).

That's because while the terminal plugin is loaded at that point, the terminal plugin installs terminalreporter exactly by implementing a pytest_configure hook:

# terminal.py
def pytest_configure(config):
    config.option.verbose -= config.option.quiet
    reporter = TerminalReporter(config, sys.stdout)
    config.pluginmanager.register(reporter, 'terminalreporter')
    ...

So what's happening is that your conftest's pytest_configure is being called before terminal's pytest_configure. The simplest solution is just to ask pytest to call your hook as late as possible:

@pytest.hookimpl(trylast=True)
def pytest_configure(config):                                                    
    assert config.pluginmanager.hasplugin('terminalreporter') 

Hope this helps! 😄

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Aug 14, 2015
@mblayman
Copy link
Author

Ah, so terminalreporter is not technically a built-in even though it's in core? I guess this is working as is so I'll close it out.

Thanks for the pointer!

@RonnyPfannschmidt
Copy link
Member

@mblayman the terminalreporter is a class instance the terminal plugin installs to implement its function,

the notion i builtin is a bit flaky, since not all builtin plugins are enabled by default (see te pytester plugin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants