Support usefixtures with parametrize#11298
Support usefixtures with parametrize#11298ShurikMen wants to merge 3 commits intopytest-dev:mainfrom
Conversation
079ca61 to
a1846a5
Compare
bluetech
left a comment
There was a problem hiding this comment.
Thanks @ShurikMen. This seems like a very nice feature to have.
The implementation doing set(fixtureinfo_.names_closure) != set(fixtureinfo.names_closure): is too hacky for my tastes; feels like a thing that will cause unexpected issues. I will try to look into what it would take to this in a more robust way. #11412 is relevant.
| and not isinstance( | ||
| inspect.getattr_static(cls, name, default=None), staticmethod | ||
| ) | ||
| and not hasattr(function, "__self__") |
There was a problem hiding this comment.
Parameterized tests are bound to an instance of the test class. And in their signature there is no longer a parameter for an instance of this class ('self').
A check on bound is needed so as not to delete the first fixture or parameter that is passed through arguments.
| @@ -0,0 +1 @@ | |||
| Support use `pytest.mark.usefixtures` with `pytest.mark.parametrize`. | |||
There was a problem hiding this comment.
An example would be cool here I think. Also mentioning this technique in the docs somewhere would be good.
|
Another problem that I forgot to mention -- what if the fixture is itself again parametrized? Adapting the example from the PR: import pytest
@pytest.fixture(params=[21, 22])
def some_fixture(request):
return request.param
@pytest.mark.parametrize("val", [
1,
pytest.param(2, marks=pytest.mark.usefixtures('some_fixture')),
3,
])
def test_foo(request, val):
assert valWith the current PR it fails: This happens because expand parametrization once, in Metafunc, in a cartesian product sort of way, while this feature requires Metafunc to be smarter. |
Perhaps a hacky, but sufficient check for the fact of changing the list of fixtures for this instance of the test with this parameter in comparison with the parent set. |
|
In theory, we can try to change the order of calling |
a1846a5 to
2ccebf3
Compare
2ccebf3 to
aa9bc87
Compare
It's worth a try to see what happens. I expect it wouldn't work, because of indirect fixtures stuff. Though maybe it's already handled at this point of execution, I don't recall ATM... |
|
@ShurikMen would you like to pick that up and try the suggested approach? Otherwise we might want to close this to be picked up later. |
|
Not relevant after changes in #11416. Now usefixtures works with parameters. |
closes #4112