I have comfortably been using @pytest.mark.parametrize() in many tests now. I have not, however, succeeded in combining it with @pytest.fixture() and I cannot find a question answering this issue.
This example applies @pytest.fixture() succesfully (copied from another question that I cannot find anymore):
import pytest
def foo(data, key):
return data[key]
@pytest.fixture()
def dict_used_by_many_tests():
return {"name": "Dionysia", "age": 28, "location": "Athens"}
def test_foo_one(dict_used_by_many_tests):
actual = foo(dict_used_by_many_tests, "name")
expected = "Dionysia"
assert actual == expected
Now, in practice I want to use @pytest.mark.parametrize().
@pytest.fixture()
def dict_used_by_many_tests():
return {"name": "Dionysia", "age": 28, "location": "Athens"}
@pytest.mark.parametrize(
"dict_used_by_many_tests",
[
(dict_used_by_many_tests),
],
)
def test_foo_one(dict_used_by_many_tests):
actual = foo(dict_used_by_many_tests, "name")
expected = "Dionysia"
assert actual == expected
This results in the error: TypeError: 'function' object is not subscriptable.
I tried calling dict_used_by_many_tests() to work with its return value instead of the function object. This resulted in a Fixtures are not meant to be called directly error, however.
One method is
request.getfixturevalue:Another is indirect parametrization: