Reputation: 3328
How do you write a fixture (a method) that yields/returns parameterized test parameters?
For instance, I have a test as the following:
@pytest.mark.parametrize(
"input,expected",
[("hello", "hello"),
("world", "world")])
def test_get_message(self, input, expected):
assert expected == MyClass.get_message(input)
Instead of having input
and expected
to be passed via @pytest.mark.parametrize
, I am interested in an approach as the following:
@pytest.fixture(scope="session")
def test_messages(self):
# what should I write here to return multiple
# test case with expected value for each?
pass
def test_get_message(self, test_messages):
expected = test_messages["expected"] # somehow extracted from test_messages?
input = test_messages["input"] # somehow extracted from test message?
assert expected == MyClass.get_message(input)
Upvotes: 1
Views: 614
Reputation: 8322
To me, it seems you want to return an array of dicts:
@pytest.fixture(scope="session")
def test_messages():
return [
{
"input": "hello",
"expected": "world"
},
{
"input": "hello",
"expected": "hello"
}
]
To use it in a test case, you would need to iterate over the array:
def test_get_message(self, test_messages):
for test_data in test_messages:
input = test_data["input"]
expected = test_data["expected"]
assert input == expected
But I not sure if this is the best approach, because it's still considered as only one test case, and so it will show up as only one test case in the output/report.
Upvotes: 1
Reputation: 16815
To move the parameters into a fixture, you can use fixture params:
@pytest.fixture(params=[("hello", "hello"),
("world", "world")], scope="session")
def test_messages(self, request):
return request.param
def test_get_message(self, test_messages):
input = test_messages[0]
expected = test_messages[1]
assert expected == MyClass.get_message(input)
You can also put the params into a separate function (same as wih parametrize
), e.g.
def get_test_messages():
return [("hello", "hello"), ("world", "world")]
@pytest.fixture(params=get_test_messages(), scope="session")
def test_messages(self, request):
return request.param
Upvotes: 2