Pytest: 'pytest_cmdline_parse' not called

Created on 22 Mar 2019  路  10Comments  路  Source: pytest-dev/pytest

Thanks for submitting an issue!

Here's a quick checklist in what to include:

  • [X] Include a detailed description of the bug or suggestion

I've got a setuptools plugin project using module-level hooks and 'pytest_cmdline_parse' is not getting called. All other bootstrapping hooks and initialization hooks are called, but not this one. I went through https://github.com/pytest-dev/pytest/issues/2616 but this is different because I am calling it from a setuptools plugin. Is there something special about this hook that I should know about? Thank you very much!

  • [X] pip list of the virtual environment you are using

apipkg 1.5
atomicwrites 1.3.0
attrs 19.1.0
colorama 0.4.1
execnet 1.5.0
funcsigs 1.0.2
more-itertools 5.0.0
pathlib2 2.3.3
pip 19.0.3
pluggy 0.9.0
py 1.8.0
pytest 4.3.1
pytest-forked 1.0.2
pytest-xdist 1.26.1
scandir 1.10.0
setuptools 40.8.0
six 1.12.0
virtualenv 16.4.3
wheel 0.33.1

  • [X] pytest and operating system versions

Pytest 4.3.1
Python 2.7.16
Windows 10 Build 1803

  • [X] Minimal example if possible

I am testing the hooks with a simple print statement:

def pytest_cmdline_parse(pluginmanager, args):
    print("pytest_cmdline_parse")
question

All 10 comments

Hi @garytyler,

Sorry for the delay. This hook is an "early load hook", meaning that it is called before we even start collecting conftest.py files (see the note in the docs).

There's an alternative: you can put your hook into a module which is importable by pytest (say mylib.early_hook) and then force it to be loaded with pytest -p mylib.early_hook, or even better, by adding addopts= -p mylib.early_hook into your pytest.ini file.

I'm closing this for now, feel free to follow up with further questions. 馃憤

@nicoddemus I am not calling them in a confest.py. I am calling them in an installed setuptools plugin, and as far as I can tell, the exception you describe is the same with all 'Initialization hooks'. Like I said, I have no problem calling any other hook. I will clone the pytest repo and inspect further if needed, but was just hoping I could get an idea from here. Thanks for you help.

@garytyler thanks for the clarification, I assumed it was from a conftest.py plugin as this is the common situation.

From what I've looked in the code, it should be calling pytest_cmdline_parse if you are using a setuptools plugin

Actually, it will call it only for builtin plugins or those parsed in the command-line:

https://github.com/pytest-dev/pytest/blob/aae02863db9652c6bf2ad97a9f0e9336f4facafb/src/_pytest/config/__init__.py#L182-L196

load_setuptools_entrypoints will be called by parse, inside the default implementation of pytest_cmdline_parse:

https://github.com/pytest-dev/pytest/blob/aae02863db9652c6bf2ad97a9f0e9336f4facafb/src/_pytest/config/__init__.py#L652-L655

So it seems there's no easy way to override this hook other than through the command line.

@nicoddemus Thank you! This is really helpful.

@garytyler a patch to the docs mentioning this particularity of this hook would be welcome if you have the time. 馃憤

@nicoddemus I am happy to help. The limitations you described disqualify this hook from suiting my purposes, but I wanted to test it before updating the docs and I'm not having any luck using this hook by loading the plugin with -p.

I've only briefly looked at the code but in the block you referenced above, plugins is None, regardless of how I load the plugin, but I can load it from command line no problem, so I'm not sure exactly where it's being loaded. I'd be happy to help and I can look into it more, especially if you have any input. Thanks again.

@nicoddemus As it stands, it looks like the only way to use this hook (outside of the built-in plugins) is actually to define it in a class that gets loaded it with pytest.main(args, plugins).

In the codeblock you reference above, plugins will be None at that point unless running pytest.main(args, plugins) or some other in-process invocation that allows passing evaluated code. I can't think of any other pytest features that are limited to the pytest.main function call off the top of my head. Most limitations go the other way around, but I've updated the docs in #5010. It looks like that was merged while I was typing this but let me know if you have any thoughts on this. It might be good to add a test for the hook?

One related edge case here is that if a plugin class that implements this hook is loaded using pytest.main(args, plugins) _within a setuptools plugin_, then it will result in a recursion loop. I could probably handle that if you'd like.

If the doc update is suitable, you can re-close this.

I think the docs are fine, thanks a lot! #5010 has not been merged yet, waiting for CI to at least clear a docs job first. 馃憤

Was this page helpful?
0 / 5 - 0 ratings