-
-
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
Should warn when using pytest_plugins
in a test file (since it's also not the root conftest.py)
#13030
Comments
So no matter where it is written, activated globally? consider removing the previous warning and displaying it in the 'plugin' of the terminal ? (Just an idea) |
I'm concerned this will cause a lot of churn for simple single-file pytest plugins, where you have a From a quick look at a few smaller pytest-dev plugins:
This kind of thing seems quite common in general: So if such a warning is introduced, I think it...
|
That's a fair concern. I like your idea to suppress the warning when there's only a single test file. There could still be issues when the plugin uses hooks that have already been run. (e.g.,
Yeah, that's what I was afraid of. Up until this week, Overall, emitting a warning if But while we're talking about this: What if we made I'm working on a proof of concept to see if it's viable. So far it seems doable, but tricky. It'd be a breaking change. But hopefully no one is intentionally relying on test files If that seems worth exploring, I'm happy to keep working on my proof of concept and share it. |
I think it's fair to warn in any case with more than one test file I think it's also fair to eventually deprecated it altogether |
pip list
from the virtual environment you are usingDescription
If I load a plugin using
pytest_plugins
in a test file, pytest should issue a warning similar to the warning given when usingpytest_plugins
in a non-rootconftest.py
.I'm opening this as a bug because this seems unintentional. When pytest started discouraging
pytest_plugins
in non-rootconftest.py
, it should have been discouraged in test files too. If folks would prefer this as a feature request, I'm happy to change it.Why this is a problem
The same issues with plugins in non-root
conftest.py
: The plugin gets activated globally for the rest of the test run, even though it seems like it'd only be activated for that one test file. (#3625 and #2062 have some discussion on pitfalls ofpytest_plugins
outside root conftest.py)Moreover, since there's now a warning/error when using
pytest_plugins
in non-root conftest.py, the lack of a warning when usingpytest_plugins
in test files implies that usingpytest_plugins
in test files doesn't have the same problems.Of course, the problems still apply to test files. From my experiments (using pytest 8.3.4):
pytest_plugins
)pytest_plugins
executespytest_plugins
)And then if the file with
pytest_plugins
is omitted from the test run (e.g., by selecting a specific file), then the plugin is not loaded at all. (This behavior is reasonable/good, but it can cause surprising/confusing test failures when combined with plugins in test files.)Minimal example
Expand for code snippets
$ pytest tests
All tests will pass, and no warning is emitted. (This template can be tweaked to experiment with the other behaviors I described.)
Other info
The text was updated successfully, but these errors were encountered: