You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
when @parametrize finds a fixture_ref in its argvalues, the parameter is not anymore accessible through request.getfixturevalue #146
Trying to figure out how to maintain the return values from cases that use a fixture as their own fixture name.
Example
conftest.py
@pytest.fixture
def some_fixture():
return 1
@pytest.fixture(scope='function', autouse=True)
def some_other_fixture(request):
# ouput will contain test_some_test_important_value_other_important_value as the fixture name
print('fixturenames: ' + str(request.node.fixturenames))
# hoping for important_value and other_important_value to be listed as fixtures
def case_some_case(some_fixture):
important_value = some_fixture
other_important_value = 2
return important_value, other_important_value
mock_test.py
@parametrize_with_cases("important_value, other_important_value", cases='.')
def test_some_test(important_value, other_important_value):
# important_value and other_important_value are here no problem
assert important_value == 1
assert other_important_value == 2
I didn't change anything in the case_ functions. While this works, it would be cool to have similar functionality for @parametrize_with_cases by having an option to prevent packing or union etc.
Hi @TheCaffinatedDeveloper , I am not sure to understand what the actual issue is.
All fixture names are preserved. However additional new fixtures are created automatically behind the scenes by @parametrize when there is at least a fixture_ref in the parameters. This happens when @parametrize_with_cases finds a case that relies on a fixture: it creates a fixture for the case, and then creates a fixture_ref pointing to it in the generated @parametrize.
So that is why you have for example test_some_test_important_value_other_important_value in the list.
If you compare the lists you should see that no name disappeared, only new names appeared. Let me know if this is not the case.
Hi @TheCaffinatedDeveloper , I am not sure to understand what the actual issue is.
All fixture names are preserved. However additional new fixtures are created automatically behind the scenes by @parametrize when there is at least a fixture_ref in the parameters. This happens when @parametrize_with_cases finds a case that relies on a fixture: it creates a fixture for the case, and then creates a fixture_ref pointing to it in the generated @parametrize.
So that is why you have for example test_some_test_important_value_other_important_value in the list.
If you compare the lists you should see that no name disappeared, only new names appeared. Let me know if this is not the case.
@smarie Thank you for your reply. That definitely makes sense. To clarify my example, if you print a list of fixtures using request.node.fixturenames the names do appear to be preserved here.
Even further, if you were use request.getfixturevalue('important_value') it would say that the fixture does not exist.
Again, this is only when a case_ is using a fixture_ref.
Thanks @TheCaffinatedDeveloper for this example ! I did not know about repl.it, quite cool :)
I copy the examples here for reproducibility
frompytest_casesimportparametrize_with_casesdefcase_without_fixture():
important_value=1other_important_value=2returnimportant_value, other_important_value@parametrize_with_cases("important_value,other_important_value", cases='.')deftest_case_without_fixture(request, important_value, other_important_value):
# important_value and other_important_value are here no problemvalue=request.getfixturevalue('important_value').get()
assertvalue==1assertother_important_value==2
and
frompytest_casesimportparametrize_with_casesdefcase_with_fixture(some_fixture):
important_value=some_fixtureother_important_value=2returnimportant_value, other_important_value@parametrize_with_cases("important_value,other_important_value", cases='.')deftest_case_with_fixture(request, important_value, other_important_value):
value=Nonetry:
value=request.getfixturevalue('important_value').get()
except:
print('Fixture not found :(')
assertvalue==important_valueassertimportant_value==1assertother_important_value==2
I think that there is a small misunderstanding here: important_value is not a fixture, this is a parameter. But since the pytest api is misleadingly named getfixturevalue it is quite normal to do the mistake :)
Ok I see the issue, and I'm afraid that this might be extremely difficult to fix. That being said, I'll have a look when I'm back to normal activity. Thanks !
Trying to reformulate this ticket in order to leave it open: when @parametrize finds a fixture_ref in its argvalues, the parameter is not anymore accessible through request.getfixturevalue.
This is due to the fact that a does not exist as a parameter in the second case. Indeed @parametrize replaces test_with_fixture_ref with a wrapper function, depending on a newly generated fixture named test_with_fixture_ref_a. We can look at the console prints above to see it in request._fixture_defs.
I'm afraid there is no simple solution to this problem... I suggest to close as "won't fix" except if you feel that this should be tackled. In that case we'll leave it open for community support
changed the title [-]cases with fixture change fixturenames[/-][+]when @parametrize finds a fixture_ref in its argvalues, the parameter is not anymore accessible through request.getfixturevalue[/+]on Dec 16, 2020
changed the title [-]when @parametrize finds a fixture_ref in its argvalues, the parameter is not anymore accessible through request.getfixturevalue[/-][+]when @parametrize finds a fixture_ref in its argvalues, the parameter is not anymore accessible through `request.getfixturevalue`[/+]on Dec 16, 2020
Activity
TheCaffinatedDeveloper commentedon Nov 14, 2020
I should note that I was able to use the follow to accomplish what I was looking for:
I didn't change anything in the case_ functions. While this works, it would be cool to have similar functionality for @parametrize_with_cases by having an option to prevent packing or union etc.
smarie commentedon Nov 15, 2020
Hi @TheCaffinatedDeveloper , I am not sure to understand what the actual issue is.
All fixture names are preserved. However additional new fixtures are created automatically behind the scenes by
@parametrizewhen there is at least afixture_refin the parameters. This happens when@parametrize_with_casesfinds a case that relies on a fixture: it creates a fixture for the case, and then creates afixture_refpointing to it in the generated@parametrize.So that is why you have for example
test_some_test_important_value_other_important_valuein the list.If you compare the lists you should see that no name disappeared, only new names appeared. Let me know if this is not the case.
TheCaffinatedDeveloper commentedon Nov 16, 2020
@smarie Thank you for your reply. That definitely makes sense. To clarify my example, if you print a list of fixtures using
request.node.fixturenamesthe names do appear to be preserved here.Even further, if you were use request.getfixturevalue('important_value') it would say that the fixture does not exist.
Again, this is only when a case_ is using a fixture_ref.
TheCaffinatedDeveloper commentedon Nov 17, 2020
@smarie I know you are busy so I created a repl to demonstrate the issue:
Example
smarie commentedon Nov 18, 2020
Thanks @TheCaffinatedDeveloper for this example ! I did not know about repl.it, quite cool :)
I copy the examples here for reproducibility
and
I think that there is a small misunderstanding here:
important_valueis not a fixture, this is a parameter. But since thepytestapi is misleadingly namedgetfixturevalueit is quite normal to do the mistake :)Ok I see the issue, and I'm afraid that this might be extremely difficult to fix. That being said, I'll have a look when I'm back to normal activity. Thanks !
TheCaffinatedDeveloper commentedon Nov 18, 2020
@smarie Thanks, glad you found REPL.it useful 👍
smarie commentedon Dec 16, 2020
Trying to reformulate this ticket in order to leave it open: when
@parametrizefinds afixture_refin itsargvalues, the parameter is not anymore accessible throughrequest.getfixturevalue.Minimal example:
This is due to the fact that
adoes not exist as a parameter in the second case. Indeed@parametrizereplacestest_with_fixture_refwith a wrapper function, depending on a newly generated fixture namedtest_with_fixture_ref_a. We can look at the console prints above to see it inrequest._fixture_defs.So the second test is equivalent to
I'm afraid there is no simple solution to this problem... I suggest to close as "won't fix" except if you feel that this should be tackled. In that case we'll leave it open for community support
[-]cases with fixture change fixturenames[/-][+]when @parametrize finds a fixture_ref in its argvalues, the parameter is not anymore accessible through request.getfixturevalue[/+][-]when @parametrize finds a fixture_ref in its argvalues, the parameter is not anymore accessible through request.getfixturevalue[/-][+]when @parametrize finds a fixture_ref in its argvalues, the parameter is not anymore accessible through `request.getfixturevalue`[/+]Moved test for #146.