I have next code for pytest:
import pytest
@pytest.fixture
def my_fixture():
def _method(a_list):
return [num*0.5 for num in a_list]
return _method
# @pytest.mark.parametrize ??
def test_me(my_fixture):
pre_list = [0, 3, 1, 2]
finally_list = my_fixture(pre_list)
for i in finally_list:
assert i < 10
The meaning is as follows. I have the initial data for the test ("pre_list"). First I need to convert them using a fixture. I did it through an additional function in the fixture, "_method(a_list)". But then the values obtained from 'finally_list' I want to use for parameterization within the current test. Is it really possible to do this? To convert the data from the test using a fixture, and then use them to parametrize the same test
While I run all the values through for:
for i in finally_list: assert i < 10
But this is not what I would like. I would like to have a separate test for each value through parameterization.
You can use Indirect parametrization .
Example:
import pytest
@pytest.fixture
def my_fixture(request):
return request.param * 0.5
@pytest.mark.parametrize('my_fixture', [0, 3, 1, 2], indirect=True)
def test_me(my_fixture):
assert my_fixture < 10
This will make sure, that all values are first passed through your fixture and be modified like you want them to and just then arrive the testcase.
The above example should give approximately the following output (depending on your plugins, where you run it, etc.) for pytest --collect-only <your_test_file.py>
:
========================================== test session starts ==========================================
platform linux -- Python 3.10.12, pytest-7.4.0, pluggy-1.3.0
rootdir: /tmp/tests
collected 4 items
<Module t.py>
<Function test_me[0]>
<Function test_me[3]>
<Function test_me[1]>
<Function test_me[2]>
====================================== 4 tests collected in 0.00s =======================================