pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.8k stars 2.62k forks source link

Collecting tests from package `app` gets broken after adding pytest plugin `app.tests.foo` #9200

Open marcinbarczynski opened 2 years ago

marcinbarczynski commented 2 years ago

Minimal example: after adding app.tests.foo to pytest_plugins in conftest.py, collecting tests fails:

$ find .
.
./conftest.py
./app
./app/tests
./app/tests/foo.py
./app/tests/app
./app/tests/app/test_app.py
./app/tests/app/__init__.py
./app/tests/app2
./app/tests/app2/test_app2.py
./app/tests/app2/__init__.py
./app/src

$ cat conftest.py 
pytest_plugins = ["app.tests.foo"]
$ cat ./app/tests/foo.py
$ cat ./app/tests/app/test_app.py
def test_sum():
    assert 2 + 2 == 4
$ cat ./app/tests/app2/test_app2.py
def test_sum():
    assert 2 + 2 == 4

$ pip freeze
attrs==21.2.0
iniconfig==1.1.1
packaging==21.0
pluggy==1.0.0
py==1.10.0
pyparsing==2.4.7
pytest==6.2.5
toml==0.10.2

$ pytest .
================================================================================================================================== test session starts ===================================================================================================================================
platform linux -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/q/dev/repro
collected 1 item / 1 error                                                                                                                                                                                                                                                               

========================================================================================================================================= ERRORS =========================================================================================================================================
_______________________________________________________________________________________________________________________ ERROR collecting app/tests/app/test_app.py _______________________________________________________________________________________________________________________
ImportError while importing test module '/home/q/dev/repro/app/tests/app/test_app.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'app.test_app'
================================================================================================================================ short test summary info =================================================================================================================================
ERROR app/tests/app/test_app.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
==================================================================================================================================== 1 error in 0.06s ====================================================================================================================================

Without pytest_plugins = ["app.tests.foo"] everything works fine.

RonnyPfannschmidt commented 2 years ago

does it by chance resolve if __init__.py files are added into app and app/tests

RonnyPfannschmidt commented 2 years ago

ah, forget that

the issue is that with the conftest the global app folder is added as first location to look after

so the name changes to app.tests.app.testapp for the details i have to look up code

cristianMeli commented 2 years ago

Hi @RonnyPfannschmidt @marcinbarczynski how's it going? This issue could be related to the Requiring/Loading plugins in a test module or conftest file Note section?. It says: _Requiring plugins using pytestplugins variable in non-root conftest.py files is deprecated.

_This is important because conftest.py files implement per-directory hook implementations, but once a plugin is imported, it will affect the entire directory tree. In order to avoid confusion, defining pytestplugins in any conftest.py file which is not located in the tests root directory is deprecated, and will raise a warning. In the example provided conftest.py is out of tests directory.

RonnyPfannschmidt commented 2 years ago

Nope, diff issue with import path handling

cristianMeli commented 2 years ago

Ok, I will work on this issue.