-
-
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
Pytest verifies if hooks are valid before processing "pytest_plugins" variable #1821
Comments
I think I'm seeing the same issue when using pytest-factoryboy (with a django project): # conftest.py
from factory import django as factory_django
import pytest_factoryboy
@pytest_factoryboy.register
class UserFactory(factory_django.DjangoModelFactory):
class Meta:
model = 'auth.User'
username = 'test_user' I get the following error:
The simple workaround is to use I don't get this error when I import I'm using pytest 3.0.6, pytest-factoryboy 1.3.0 (although I confirmed that the same error happened with several prior releases) and pytest-django 3.1.2. |
oh - @nicoddemus the behaviour as seen is correct, hooks that may not be declared in plugin order must be marked as optional |
@nicoddemus @RonnyPfannschmidt I just ran into this as well with packaging our new project pytestlab. I'm wondering is maybe
|
Guys unless I'm misunderstanding this might be actually kind of bad. Currently Therefore hook validation is never done except for the initial plugin set loaded by This also makes me wonder if hook validation is something that should be done on every plugin loaded with |
@tgoodlet potentially yes, this may need a discussion/brainstorm |
@RonnyPfannschmidt I've opened pytest-dev/pluggy#48 for a follow up regarding the validation question. What do you think about the recursive loading suggestion I made? |
Here are the updated files: # content of conftest.py
pytest_plugins = ['plugin']
def pytest_my_hook(config):
assert 0
# content of test_foo.py
def test(request):
request.config.hook.pytest_my_hook(config=request.config)
# content of plugin.py
class Hooks:
def pytest_my_hook(self, config):
pass
def pytest_configure(config):
config.pluginmanager.add_hookspecs(Hooks) I think this can be fixed by calling diff --git a/_pytest/config.py b/_pytest/config.py
index c73416f..8af3908 100644
--- a/_pytest/config.py
+++ b/_pytest/config.py
@@ -53,7 +53,7 @@ def main(args=None, plugins=None):
return 4
else:
try:
- config.pluginmanager.check_pending()
+
return config.hook.pytest_cmdline_main(config=config)
finally:
config._ensure_unconfigure()
diff --git a/_pytest/main.py b/_pytest/main.py
index b66b661..1bc80b2 100644
--- a/_pytest/main.py
+++ b/_pytest/main.py
@@ -598,6 +598,7 @@ class Session(FSCollector):
hook = self.config.hook
try:
items = self._perform_collect(args, genitems)
+ self.config.pluginmanager.check_pending()
hook.pytest_collection_modifyitems(session=self,
config=self.config, items=items)
finally: This gives a chance for all plugins to be loaded and validated properly. We then don't need the slighly hackish way of deferring the use of hooks. |
With the above patch, my example works (the assertion inside |
@nicoddemus nice, yes this is the fix I wanted to see and I think it addresses the bulk of the issue :) I still think it's not necessarily great that validation only ever happens once. I feel like we should at least allow the plugin provider the option to specify how "strict" (or maybe how often?) validations should be. |
Great, then unless @RonnyPfannschmidt has any objections I will put together a PR.
Not that I'm aware of, but I'm not 100% sure either.
I see, thanks. Do you have an idea on how |
@nicoddemus see my comments in pytest-dev/pluggy#48 for a deeper explanation :) |
Consider:
This produces this error:
It seems pytest is validating hooks before actually processing all hooks.
This is using the
features
branch just pre-release of 3.0 (be9356a).The text was updated successfully, but these errors were encountered: