Skip to content

conftest.py discovery when running pytest --rootdir #11071

Description

@piotr-kubiak

My setup:

(.venv) [root@4bd7686ed675 git_root]# pwd
/root/git_root
(.venv) [root@4bd7686ed675 git_root]# tree
.
├── important_folder
├── other_folder
└── package_root
    ├── conftest.py
    ├── src
    │   └── my_package
    │       ├── __init__.py
    │       └── main.py
    └── tests
        ├── __init__.py
        └── test_main.py

(.venv) [root@4bd7686ed675 git_root]# pip list
Package        Version
-------------- -------
exceptiongroup 1.1.1
iniconfig      2.0.0
packaging      23.1
pip            21.2.3
pluggy         1.0.0
pytest         7.3.1
setuptools     53.0.0
tomli          2.0.1

The contents of conftest.py:

(.venv) [root@4bd7686ed675 git_root]# cat package_root/conftest.py 
pytest_plugins = ""

All other files are empty.
Let's run pytest:

(.venv) [root@4bd7686ed675 git_root]# pytest
============================================================================== test session starts ==============================================================================
platform linux -- Python 3.9.16, pytest-7.3.1, pluggy-1.0.0
rootdir: /root/git_root
collected 0 items / 1 error                                                                                                                                                     

==================================================================================== ERRORS =====================================================================================
_________________________________________________________________________ ERROR collecting test session _________________________________________________________________________
Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:
It affects the entire test suite instead of just below the conftest as expected.
  /root/git_root/package_root/conftest.py
Please move it to a top level conftest file at the rootdir:
  None
For more information, visit:
  https://docs.pytest.org/en/stable/deprecations.html#pytest-plugins-in-non-top-level-conftest-files
============================================================================ short test summary info ============================================================================
ERROR 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================================================== 1 error in 0.09s ================================================================================

Ok, that is an error, because conftest.py is not in rootdir: /root/git_root. Let's try to fix it:

(.venv) [root@4bd7686ed675 git_root]# pytest --rootdir=/root/git_root/package_root/
============================================================================== test session starts ==============================================================================
platform linux -- Python 3.9.16, pytest-7.3.1, pluggy-1.0.0
rootdir: /root/git_root/package_root
collected 0 items                                                                                                                                                               
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/main.py", line 269, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/main.py", line 322, in _main
INTERNALERROR>     config.hook.pytest_collection(session=session)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/main.py", line 333, in pytest_collection
INTERNALERROR>     session.perform_collect()
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/main.py", line 646, in perform_collect
INTERNALERROR>     rep = collect_one_node(self)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/runner.py", line 545, in collect_one_node
INTERNALERROR>     ihook = collector.ihook
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/nodes.py", line 267, in ihook
INTERNALERROR>     return self.session.gethookproxy(self.path)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/main.py", line 542, in gethookproxy
INTERNALERROR>     my_conftestmodules = pm._getconftestmodules(
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/config/__init__.py", line 602, in _getconftestmodules
INTERNALERROR>     mod = self._importconftest(conftestpath, importmode, rootpath)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/config/__init__.py", line 640, in _importconftest
INTERNALERROR>     self._check_non_top_pytest_plugins(mod, conftestpath)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/config/__init__.py", line 672, in _check_non_top_pytest_plugins
INTERNALERROR>     fail(msg.format(conftestpath, self._confcutdir), pytrace=False)
INTERNALERROR>   File "/root/git_root/.venv/lib64/python3.9/site-packages/_pytest/outcomes.py", line 198, in fail
INTERNALERROR>     raise Failed(msg=reason, pytrace=pytrace)
INTERNALERROR> Failed: Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:
INTERNALERROR> It affects the entire test suite instead of just below the conftest as expected.
INTERNALERROR>   /root/git_root/package_root/conftest.py
INTERNALERROR> Please move it to a top level conftest file at the rootdir:
INTERNALERROR>   None
INTERNALERROR> For more information, visit:
INTERNALERROR>   https://docs.pytest.org/en/stable/deprecations.html#pytest-plugins-in-non-top-level-conftest-files

Unfortunately, still an error, which seems like a bug to me, because now conftest.py is in the rootdir.
Let's change current dir and try again:

(.venv) [root@4bd7686ed675 git_root]# cd package_root/
(.venv) [root@4bd7686ed675 package_root]# pytest --rootdir=/root/git_root/package_root/
============================================================================== test session starts ==============================================================================
platform linux -- Python 3.9.16, pytest-7.3.1, pluggy-1.0.0
rootdir: /root/git_root/package_root
collected 0 items                                                                                                                                                               

============================================================================= no tests ran in 0.01s =============================================================================

I suppose that the output of pytest --rootdir=<dir> should not be dependent on current working directory.

Some background:
While working from package_root/ seems like a workaround, this is not convenient enough for me. My company requires me to put some PM-related stuff inside our git repositories (important_folder/). We are then using VS Code to open git_root/ as a workspace, and work from there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions