pytest
pytest copied to clipboard
Provide a better way to schedule hooks ordering
What's the problem this feature will solve?
Provide a better way to fine-tune the order in which different hooks are executed.
Currently, the only way to control the scheduling is through the following parameters of the @pytest.hookimpl marker:
- tryfirst=True
- tryfirst=False
- trylast=True
- trylast=False
What if I want my plugin hook to be executed after plugin-A and plugin-B but before plugin-X and plugin-Y hooks ?
Describe the solution you'd like
Difficult to say what the ideal solution might be. Maybe something like this ?
@pytest.hookimpl(after=['plugin-A', 'plugin-B'], before=['plugin-X', 'plugin-Y'])
Of course, ideally we should check if the other plugins are registered/installed. If a plugin is not registered, then the hook condition should be considered as being fulfilled.
example:
If plugin-A is not registered/installed:
@pytest.hookimpl(after=['plugin-A', 'plugin-B'], before=['plugin-X', 'plugin-Y'])
should behave like
@pytest.hookimpl(after=['plugin-B'], before=['plugin-X', 'plugin-Y'])
Another consideration:
If there is an impossible order to achieve, let say plugin-X asks its hooks to be executed before plugin-A hooks, then
@pytest.hookimpl(after=['plugin-A', 'plugin-B'], before=['plugin-X', 'plugin-Y']) cannot be honored and we should display a message in the stderr output.
Maybe also abort the execution ? or let the execution but ignoring the hook ordering ?
Alternative Solutions
Additional context
I don't think this is worth the complexity.
We have four describable cases: before, after, not-after, and not-before - where the first two require the referenced plugin to be loaded, and the latter do not.
a before b(or symetricallyb after a): a must be doing some important setup for b (else this would be not-after); b should explicitly import and use that code if necessary (and arrange for idempotence or a setup_if_needed method).a not-after b, andb not-before a: b is presumably using or polluting some state which a requires. IMO a should have better isolation, or b is being a poor ecosystem citizen and users should disable it.
This os a issue for pluggy
Having more order controls is a long standing issue
This may be a dupe of #2364