Skip to content
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

The plugin hooks pytest_cmdline_parse and pytest_load_initial_conftests don't appear to get called #2616

Closed
ceridwen opened this issue Jul 25, 2017 · 4 comments
Labels
topic: collection related to the collection phase type: docs documentation improvement, missing or needing clarification

Comments

@ceridwen
Copy link
Contributor

ceridwen commented Jul 25, 2017

I have a conftest.py I've been using to try to test the available hook functions.

import pytest

print('conftest.py')

def pytest_load_initial_conftests(early_config, parser, args):
    print(early_config)
    print(parser)
    print(args)

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

def pytest_cmdline_main(config):
    print(config)

def pytest_cmdline_preparse(config, args):
    print(config)
    print(args)

The values for pytest_cmdline_main and pytest_cmdline_preparse never get printed, so I think they're not being called. When I look in the debugger, the latter hooks seem to be called by the line

return pluginmanager.hook.pytest_cmdline_parse(
. Setting a breakpoint there and looking at the plugins,

Pdb) b /usr/local/lib/python3.6/site-packages/_pytest/config.py:157
Breakpoint 1 at /usr/local/lib/python3.6/site-packages/_pytest/config.py:157
(Pdb) c
> /usr/local/lib/python3.6/site-packages/_pytest/config.py(157)_prepareconfig()
-> return pluginmanager.hook.pytest_cmdline_parse(
(Pdb) for i in config.hook.pytest_cmdline_parse._nonwrappers: i.function.__module__
'_pytest.config'

The only hook at this point is _pytest.config.Config.pytest_cmdline_parse, which gets called. Later on, the hook in my conftest.py is added, but I don't think this list of hooks is examined again after this initial call. Having __init__.py in the directory or not doesn't seem to matter, the first two hooks don't get executed. I haven't checked more complicated possible package configurations.

If this is intended behavior, it's not very transparent, and the documentation doesn't explain what's needed to get the first two hooks actually called.

$ pip list
asn1crypto (0.22.0)
Babel (2.3.4)
certifi (2017.4.17)
cffi (1.10.0)
chardet (3.0.4)
cryptography (1.9)
debtcollector (1.14.0)
enum34 (1.1.6)
Fabric (1.13.2)
funcsigs (1.0.2)
httplib2 (0.10.3)
idna (2.5)
ipaddress (1.0.18)
iso8601 (0.1.11)
keystoneauth1 (2.21.0)
kubernetes (2.0.0)
mercurial (4.2.1)
monotonic (1.3)
msgpack-python (0.4.8)
netaddr (0.7.19)
netifaces (0.10.6)
oauth2client (4.1.1)
oslo.i18n (3.15.3)
oslo.serialization (2.18.0)
oslo.utils (3.26.0)
paramiko (2.1.2)
pbr (3.0.1)
pexpect (4.2.1)
pip (9.0.1)
positional (1.1.1)
prettytable (0.7.2)
ptyprocess (0.5.1)
py (1.4.34)
pyasn1 (0.2.3)
pyasn1-modules (0.0.9)
pycparser (2.17)
pyparsing (2.2.0)
pytest (3.1.2)
python-dateutil (2.6.0)
python-novaclient (9.0.0)
pytz (2017.2)
PyYAML (3.12)
Reindent (0.1.1)
requests (2.17.3)
rsa (3.4.2)
setuptools (32.1.0)
simplejson (3.10.0)
six (1.10.0)
stevedore (1.23.0)
urllib3 (1.21.1)
websocket-client (0.44.0)
wheel (0.29.0)
wrapt (1.10.10)
$ pytest --version
This is pytest version 3.1.1, imported from /usr/local/lib/python3.6/site-packages/pytest.py
setuptools registered plugins:
  pytest-metadata-1.5.0 at ~/Library/Python/3.6/lib/python/site-packages/pytest_metadata/plugin.py
  pytest-html-1.15.1 at ~/Library/Python/3.6/lib/python/site-packages/pytest_html/plugin.py
  pytest-xprocess-0.12.1 at /usr/local/lib/python3.6/site-packages/pytest_xprocess.py
  pytest-cov-2.5.1 at /usr/local/lib/python3.6/site-packages/pytest_cov/plugin.py

This is on MacOS X 10.12.5 and Python 3.6 installed with Homebrew.

@nicoddemus
Copy link
Member

Hi @ceridwen,

Where is conftest.py located? pytest_load_initial_conftests and pytest_cmdline_parse are a little special in the sense that they are only called for installed plugins or conftest.py files at the root of the suite, see the note for pytest_addoption, I suspect the same rules are valid for pytest_load_initial_conftests and pytest_cmdline_parse.

@ceridwen
Copy link
Contributor Author

In this case, conftest.py is in the root of the suite. This is a minimalistic set of tests I keep around for studying pytest functionality, so there's no setup.py or other complicated arrangement of packages. If there needs to be a specific arranagement to get these two methods to work, that's not obvious to me from reading the documentation.

@nicoddemus nicoddemus added topic: collection related to the collection phase type: question general question, might be closed after 2 weeks of inactivity status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity labels Sep 28, 2017
@j19sch
Copy link

j19sch commented Jan 6, 2018

Am having the same problem using pytest 3.3.1 and python 2.7.12.

I created a basic setup similar to @ceridwen with one directory containing conftest.py and two files with tests. pytest_cmdline_preparse works, pytest_load_initial_conftests and pytest_cmdline_parse don't seem to do anything.

@nicoddemus
Copy link
Member

Looking at the code I see that this is how it works right now: conftest.py files are collected and setup to handle hooks by an implementation of the pytest_load_initial_conftests hook in config.py, so it is not possible for conftest.py files to implement that hook themselves. pytest_cmdline_parse is called way before that, which also explains why it is not being called for conftest.py files.

I don't see how it would be possible to make pytest call those hooks for conftest.py files so it's a documentation issue, those hooks (and possibly others) cannot be implemented in conftest.py files, only by setuptools plugins. ,

Opened #3091 to fix the docs.

Thanks guys for the report!

@nicoddemus nicoddemus added type: docs documentation improvement, missing or needing clarification and removed status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: question general question, might be closed after 2 weeks of inactivity labels Jan 6, 2018
alanbato pushed a commit to alanbato/pytest that referenced this issue Jan 24, 2018
alanbato pushed a commit to alanbato/pytest that referenced this issue Jan 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase type: docs documentation improvement, missing or needing clarification
Projects
None yet
Development

No branches or pull requests

3 participants