Open
Description
- a detailed description of the bug or problem you are having
When importing a package for its plugin, pytest lets any warnings fall to the ground. This means that subsequent imports in tests do not get any warnings, meaning it's possible to miss deprecation warnings. We ran into this in python-trio/trio#3053. I'm not certain this is a bug, but it's certainly surprising and I don't remember seeing anything about this while we were adding our custom plugin!
- output of
pip list
from the virtual environment you are using
Package Version
--------- -------
colorama 0.4.6
example 0.0.1
iniconfig 2.0.0
packaging 24.1
pip 24.0
pluggy 1.5.0
pytest 8.3.2
- pytest and operating system versions
As shown above, pytest is 8.3.2. I'm reproducing this on Windows 11, but it happened in CI for us for every base actions runner OS + Alpine Linux.
- minimal example if possible
pyproject.toml
:
[build-system]
requires = ["flit_core>=3.2,<4"]
build-backend = "flit_core.buildapi"
[project]
name = "example"
dynamic = ["version", "description"]
test.py
:
import example
def test_version():
assert example.__version__ == "0.0.1"
src/example/__init__.py
:
"""example"""
__version__ = "0.0.1"
import warnings
warnings.warn(DeprecationWarning("example"))
With that in place, run pip install . pytest
and then these two commands have conflicting results:
(.venv) PS C:\Users\A5rocks\Documents\pytest-repro> pytest test.py
================================================= test session starts =================================================
platform win32 -- Python 3.12.4, pytest-8.3.2, pluggy-1.5.0
rootdir: C:\Users\A5rocks\Documents\pytest-repro
configfile: pyproject.toml
collected 1 item
test.py . [100%]
================================================== warnings summary ===================================================
.venv\Lib\site-packages\example\__init__.py:6
C:\Users\A5rocks\Documents\pytest-repro\.venv\Lib\site-packages\example\__init__.py:6: DeprecationWarning: example
warnings.warn(DeprecationWarning("example"))
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============================================ 1 passed, 1 warning in 0.05s =============================================
(.venv) PS C:\Users\A5rocks\Documents\pytest-repro> pytest test.py -p example
================================================= test session starts =================================================
platform win32 -- Python 3.12.4, pytest-8.3.2, pluggy-1.5.0
rootdir: C:\Users\A5rocks\Documents\pytest-repro
configfile: pyproject.toml
collected 1 item
test.py . [100%]
================================================== 1 passed in 0.04s ==================================================