-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Surprisingly, pytest doesn't collect warnings when importing the package for plugins #12697
Comments
Hi, I want try to explain the difference in outcomes between the two execution methods: The warning is captured during the collection and execution of test cases after pytest is initialized and ready. If you execute
pytest will import If you execute
pytest will first import the The root cause is that Python ignores the If you want to display warnings regardless, you can:
If you want to ensure that warnings are not displayed under any circumstances, you can:
|
I'm aware of why this currently doesn't work, but it isn't immediately obvious and has caused (at least) one case where tests didn't match reality. Specifically, shouldn't it be possible for pytest to capture warnings from plugin imports and apply its warning filter on them? That isn't status quo, yes, but I think that would be a less surprising place for pytest to be. (Though maybe trio's case of using a plugin instead of conftest.py for privacy reasons is unique? And so this isn't worth it?) |
Right, If Python, as the upstream, and the plugins, as the downstream, didn't do this, it is debatable for pytest to do so on its own. |
pytest should enact its warning filters before starting to import from entrypoitns/given plugins of possible |
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!
pip list
from the virtual environment you are usingAs 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.
pyproject.toml
:test.py
:src/example/__init__.py
:With that in place, run
pip install . pytest
and then these two commands have conflicting results:The text was updated successfully, but these errors were encountered: