Skip to content

pytest warnings not working as expected when ran from src #65

@rsyring

Description

@rsyring

Warnings are still not working like I expected them to. I ran into this problem in ME:

 ❯ pytest -k test_decimal_roundtrip
=========================================================================== test session starts ============================================================================
platform linux -- Python 3.12.10, pytest-8.4.1, pluggy-1.5.0
rootdir: /home/rsyring
configfile: pytest.ini
plugins: typeguard-4.4.2, requests-mock-1.12.1, anyio-4.9.0, cov-6.0.0
collected 0 items / 1 error                                                                                                                                                

================================================================================== ERRORS ==================================================================================
_____________________________________________________________ ERROR collecting projects/moleliminator-src/src ______________________________________________________________
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.
  /home/rsyring/projects/moleliminator-src/src/conftest.py
Please move it to a top level conftest file at the rootdir:
  /home/rsyring
For more information, visit:
  https://docs.pytest.org/en/stable/deprecations.html#pytest-plugins-in-non-top-level-conftest-files
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================= 1 error in 0.12s =============================================================================

 rc 2 (moleliminator-src) moleliminator-src  server-migration #############################################################################❯ py 3.12.10 is 📦 v1.0 @ 15:44 
 ❯ pytest -k test_decimal_roundtrip src/
=========================================================================== test session starts ============================================================================
platform linux -- Python 3.12.10, pytest-8.4.1, pluggy-1.5.0
rootdir: /home/rsyring
configfile: pytest.ini
plugins: typeguard-4.4.2, requests-mock-1.12.1, anyio-4.9.0, cov-6.0.0
collecting ... /home/rsyring/.cache/uv-venvs/moleliminator-src/lib/python3.12/site-packages/webob/compat.py:5: DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13
  from cgi import parse_header
/home/rsyring/.cache/uv-venvs/moleliminator-src/lib/python3.12/site-packages/passlib/utils/__init__.py:854: DeprecationWarning: 'crypt' is deprecated and slated for removal in Python 3.13
  from crypt import crypt as _crypt
collected 692 items / 691 deselected / 1 selected                                                                                                                          

src/moleliminator/tests/test_app.py .                                                                                                                                [100%]

==================================================================== 1 passed, 691 deselected in 2.34s =====================================================================

 3s (moleliminator-src) moleliminator-src  server-migration ###############################################################################❯ py 3.12.10 is 📦 v1.0 @ 15:44 
 ❯ pytest -k test_decimal_roundtrip src/moleliminator
=========================================================================== test session starts ============================================================================
platform linux -- Python 3.12.10, pytest-8.4.1, pluggy-1.5.0
rootdir: /home/rsyring
configfile: pytest.ini
plugins: typeguard-4.4.2, requests-mock-1.12.1, anyio-4.9.0, cov-6.0.0
collected 692 items / 691 deselected / 1 selected                                                                                                                          

src/moleliminator/tests/test_app.py .                                                                                                                                [100%]

==================================================================== 1 passed, 691 deselected in 0.24s =====================================================================

src/conftest.py:

"""
This conftest mostly for handling warnings.  Use the other conftest.py files for app/test config.

- Filters for warnings that are triggered during import go at the top level.
- Filters for warnings thrown during test runs goes in pytest_configure() below.

Having two conftest.py files is necessary because the warning configuration needs to happen before
the application's tests and/or code have a chance to import other libraries which may trigger
warnings.  So this file remains a filesystem level above the "real" conftest.py which does all the
imports.
"""

import warnings


# Treat any warning issued in a test as an exception so we are forced to explicitly handle or
# ignore it.
warnings.filterwarnings('error')
# This warning will go away once we move to Py 3.13:
# https://github.com/Pylons/webob/issues/437#issuecomment-2658103581
warnings.filterwarnings(
    'ignore',
    "'cgi' is deprecated and slated for removal in Python 3.13",
    category=DeprecationWarning,
    module='webob.compat',
)
# https://github.com/kvesteri/sqlalchemy-utils/issues/646
warnings.filterwarnings(
    'ignore',
    "'crypt' is deprecated and slated for removal in Python 3.13",
    category=DeprecationWarning,
    module='passlib.utils',
)
# https://github.com/level12/keg/issues/202
warnings.filterwarnings(
    'ignore',
    "'MultiCommand' is deprecated and will be removed in Click 9.0. Use 'Group' instead.",
    category=DeprecationWarning,
)
###########
# REMINDER: when adding an ignore, add an issue to track it
###########


# Has to be declared in the "root" conftest
pytest_plugins = ('celery.contrib.pytest',)


def pytest_configure(config):
    """
    You may be able to do all your ignores above.  If you find some warnings need to be ignored
    in pytest, you can do that with something like:

        config.addinivalue_line(
            'filterwarnings',
            # Note the lines that follow are implicitly concatinated, no "," at the end
            'ignore'
            ':pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json'
            ':DeprecationWarning'
            ':wtforms.meta',
        )
    """
    config.pluginmanager.set_blocked('warnings')

This demonstrates that when running tests from ./src our warnings settings are not being used. That's true even though we know that file is being loaded b/c the warnings are showing in mixed in with the output, which means blocking the warnings plugin worked.

But, from a directory below src makes the warnings get hidden as intended. Crazy and opaque. :o/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions