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
12.02k stars 2.67k forks source link

Cannot use configs in non root directory #11705

Closed ktthross closed 4 months ago

ktthross commented 10 months ago

Hello there. We want to disable certain directories from running and sometimes specific files. We have conftest.py to disable specific files and a pytest.ini to disable directories from being tested. We cannot disable the tests themselves because pytest will import the test modules and in certain environments on our team, the imports are not available so the test will fail. We invoke pytest and pass the -c argument for both files along with the --rootdir argument. When pytest.ini and conftest.py are in the root directory everything works as expected. When they are in the subdirectory pytest fails to use the config files.

This command works

pytest -c conftest.py -c pytest.ini --rootdir=/home/kthrossell/working/test_pytest
=================================================================== test session starts ====================================================================
platform linux -- Python 3.10.9, pytest-7.3.1, pluggy-1.0.0
rootdir: /home/kthrossell/working/test_pytest
configfile: pytest.ini
collected 2 items                                                                                                                                          

test_devlogs.py .                                                                                                                                    [ 50%]
test/test_pi.py .                                                                                                                                    [100%]

==================================================================== 2 passed in 0.01s =====================================================================

This command does not work

pytest -c configs/conftest.py -c configs/pytest.ini --rootdir=/home/kthrossell/working/test_pytest
=================================================================== test session starts ====================================================================
platform linux -- Python 3.10.9, pytest-7.3.1, pluggy-1.0.0
rootdir: /home/kthrossell/working/test_pytest
configfile: configs/pytest.ini
collected 2 items / 1 error                                                                                                                                

========================================================================== ERRORS ==========================================================================
____________________________________________________________ ERROR collecting test/test_tau.py _____________________________________________________________
ImportError while importing test module '/home/kthrossell/working/test_pytest/test/test_tau.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../miniconda3/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test/test_tau.py:3: in <module>
    import non_existent_package
E   ModuleNotFoundError: No module named 'non_existent_package'
================================================================= short test summary info ==================================================================
ERROR test/test_tau.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================================== 1 error in 0.04s =====================================================================

Here is a gitrepo that contains the files for this example https://github.com/ktthross/test_pytest

Pip list

Package Version Editable project location


awscli 1.27.125 blinker 1.6.2 boltons 23.0.0 boto3 1.26.107 botocore 1.29.125 brotlipy 0.7.0 certifi 2022.12.7 cffi 1.15.1 charset-normalizer 2.0.4 click 8.1.3 colorama 0.4.4 conda 23.3.1 conda-content-trust 0.1.3 conda-package-handling 2.0.2 conda_package_streaming 0.7.0 contourpy 1.0.7 cryptography 39.0.1 cycler 0.11.0 Cython 0.29.34 docutils 0.16 exceptiongroup 1.1.1 flake8 6.0.0 Flask 2.3.2 Flora 0.0.0 /home/kthrossell/working/flora fonttools 4.39.4 gitdb 4.0.10 GitPython 3.1.31 grpcio 1.56.2 grpcio-tools 1.56.2 idna 3.4 iniconfig 2.0.0 itsdangerous 2.1.2 Jinja2 3.1.2 jmespath 1.0.1 jsonpatch 1.32 jsonpointer 2.1 kiwisolver 1.4.4 MarkupSafe 2.1.3 matplotlib 3.7.1 mccabe 0.7.0 numpy 1.24.2 packaging 23.0 Pillow 9.5.0 pip 23.3.1 pluggy 1.0.0 project-euler 1.2.59 protobuf 4.23.4 pyasn1 0.5.0 pycodestyle 2.10.0 pycosat 0.6.4 pycparser 2.21 pyflakes 3.0.1 pyOpenSSL 23.0.0 pyparsing 3.0.9 PySocks 1.7.1 pytest 7.3.1 python-dateutil 2.8.2 PyYAML 5.4.1 requests 2.28.1 rsa 4.7.2 ruamel.yaml 0.17.21 ruamel.yaml.clib 0.2.6 s3transfer 0.6.0 satellite 0.3.6 /home/kthrossell/working/satellite SCons 4.5.2 setuptools 65.6.3 six 1.16.0 smmap 5.0.0 test-pytest 0.1.0 /home/kthrossell/working/test_pytest tomli 2.0.1 toolz 0.12.0 tqdm 4.65.0 urllib3 1.26.15 Werkzeug 2.3.6 wheel 0.37.1 zstandard 0.19.0

pytest verions and OS

pytest -V pytest 7.3.1

This has also been tested on 7.4.0

Distributor ID: Ubuntu Description: Ubuntu 20.04.6 LTS Release: 20.04 Codename: focal

bluetech commented 10 months ago

Hi,

pytest -c configs/conftest.py -c configs/pytest.ini doesn't work, there can only be one config file, and a conftest file is not a config file in any case. So remove the -c configs/conftest.py part.

Next, to have the conftest file loaded, move it to the test directory where it's needed, that is test_pytest/conftest.py.

github-actions[bot] commented 9 months ago

This issue is stale because it has been open for 14 days with no activity.

obestwalter commented 4 months ago

Stale for a long time and no-follow up, just helping the bot here a bit and closing manually.