With fixture as argument to another fixture using params it failed with:
E TypeError: 'function' object is not iterable
But use a normal function return value works fine. This works fine:
@pytest.fixture(params=data_func())
This failed:
@pytest.fixture(params=data_fixture)
Note this is an example. My actual script reads a file located in the package root. I would like to retrieve the package root via pytestconfig.rootpath. Currently I worked around this issue using environment variable but I would prefer not having to do this as I have to come up with different variable names for each project. It would be better to retrieve it from pytest if possible. I appreciate any ideas on this. Thanks.
This script generate a list of tests using the sample input:
[('mistral', 'func1'), ('mistral', 'func2'), ('llama2', 'func1'), ('llama2', 'func2')]
The following script works fine:
import pytest
def data_func():
models = [
"mistral",
"llama2",
]
funcs = [
"func1",
"func2",
]
mylist = [(m, f) for m in models for f in funcs]
print(mylist)
return mylist
@pytest.fixture(params=data_func())
def myargs(request):
yield request.param
def test_a(myargs):
model, func = myargs
print(f"model {model} func {func}")
testings/testings6/tests/ques1.py ✓ 25% ██▌ model mistral func func1
testings/testings6/tests/ques1.py ✓✓ 50% █████ model llama2 func func1
testings/testings6/tests/ques1.py ✓✓✓ 75% ███████▌ model llama2 func func2
testings/testings6/tests/ques1.py ✓✓✓✓ 100% ██████████
But using fixture as input to params failed using both yield statement:
import pytest
@pytest.fixture
def data_fixture():
models = [
"mistral",
"llama2",
]
funcs = [
"func1",
"func2",
]
mylist = [(m, f) for m in models for f in funcs]
return mylist
@pytest.fixture(params=data_fixture)
def myargs(request):
print(request)
#yield request.param
yield request.getfixturevalue(request.param)
def test_a(myargs):
model, func = myargs
print(f"model {model} func {func}")
pytest = <module 'pytest' from '/home/kenneth/learning/venv_latest/lib/python3.10/site-packages/pytest/__init__.py'>
../../../venv_latest/lib/python3.10/site-packages/_pytest/fixtures.py:1324: in fixture
params=tuple(params) if params is not None else None,
E TypeError: 'function' object is not iterable
A few comments
Here is a suggestion:
Output
Update
I see what you are trying to do: locate the external data files. By using a fixture to generate test data, you have access to the
pytestconfigfixture, from which you can determine the root withpytestconfig.rootpath.The problem is there is no way to pass this root path to
data_func(). It is a catch 22 problem.A work-around is to move the data files into the same directory where the test reside, that way, we know how to locate them. Supposed that the data reside in models.json and funcs.json, then here is how I approach this problem:
Notes
hereitertools.productcall