Repository navigation
Description
Hi, this is a feature suggestion, which I believe is the missing piece of parametrization
We can define an individual parameter used for parametrization that is not either intrinsically tied to a fixture or test function like this:
@fixture(params=[1,2,3,4])
def my_parameter(request):
return request.param
@fixture
def fixture_uses_param(my_parameter):
pass
def test_uses_param(my_parameter, fixture_uses_param):
pass
The example above is bad because we are using a fixture to mask what is really a parameter. But we can solve this by doing:
def parameter(values):
@pytest.fixture(scope="session", params=params)
def parameter_fixture(request):
return request.param
return parameter_fixture
and then the test becomes:
my_parameter = parameter([1,2,3,4])
@fixture
def fixture_uses_param(my_parameter):
pass
def test_uses_param(my_parameter, fixture_uses_param):
pass
And we can use many parameters to have a cartesian product, etc etc. This still has two problems though:
- It does not use the string definition of arguments. In other words, maybe it would be preferred to have
parameter("my_parameter", [1,2,3,4])instead ofmy_parameter = parameter([1,2,3,4]) - I cannot parametrize a pair of arguments like this. This is related to the previous bullet point as we would need the alternate syntax for this:
parameter("my_parameter1, my_parameter2", [(1,2),(3,4)]).
So, the feature proposal would be to have this parametrization ability through the module body with the mentioned syntax:
parameter("my_parameter, my_other_parameter", [(1,1), (2,2), (3,3), (4,4)])
@fixture
def fixture_uses_param(my_parameter):
pass
def test_uses_param(my_parameter, my_other_parameter, fixture_uses_param):
pass
Is this even possible? I know very little about how parametrization works under the hood in pytest, so if you might be not interested in this feature, I would appreciate some pointers.
thanks!
Activity
smarie commentedon Mar 22, 2019
I am not sure that I understand what you wish to do here.
Is this that you wish to reuse parameters for multiple tests ? Or that you dislike the
@pytest.mark.parametrizesyntax so much that you wish to create an alias ? ;)Sup3rGeo commentedon Mar 22, 2019
No no, I like parametrize, but it injects the parameter for the specific test or fixture it is decorating.
That`s it, or reuse it in a fixture and a test, which is the first two code snippets I presented, which you can already do today. But what I can't do is have that for parametrizing a pair/triple/n-uple of parameters.
smarie commentedon Mar 22, 2019
I see. Well yes I guess that the answer to your need is indeed a "parameter fixture" as you show.
We could imagine a
param_fixturehelper function (basically a renaming of your firstparameterfunction above). But as you point out we would have to extend it to support n-tuples. This is relatively easy: we would simply have to create several fixtures depending on each other (whatever the order). I can give it a try next week, we'll see if that gets adopted :)smarie commentedon Mar 25, 2019
I managed to implement it but in case of several parameter names, I had to create a "root" fixture above all parameters. Anyway it works as expected, I'll ship 1.5.0 so that you can give it a try.
Here is an example:
smarie commentedon Mar 25, 2019
Note: may I kindly ask you to support these new features into the main
pytestproject (I saw that you opened a thread already) if they suit your needs ? Indeed it would be better for everyone I think, rather than keeping them here.Sup3rGeo commentedon Mar 25, 2019
Thanks, I will check it out (and probably make an issue if I find anything wrong)!
And sure I also believe this should eventually get into pytest itself, as many people are using this pattern of creating fixture parameters and this makes it more explicit.
smarie commentedon Mar 26, 2019
perfect! let me know