Reputation: 96
I'm trying to parametrize pytest test with a function that yields values of parameters. I want it to run test separately for each value. Here is example code:
def provider():
yield pytest.param([1, 2])
class Test:
@pytest.mark.parametrize("a", provider())
def test(self, a, logger):
logger.info("a: {}".format(a))
@pytest.mark.parametrize("a", [1, 2])
def test_2(self, a, logger):
logger.info("a: {}".format(a))
Is it possible to parametrize test using provider function to make it work the way like test_2.
Here are my logs from above tests:
2019-12-09 14:39:36 test[a0] start
a: [1, 2]
2019-12-09 14:39:36 test[a0] passed
2019-12-09 14:39:36 test_2[1] start
a: 1
2019-12-09 14:39:36 test_2[1] passed
2019-12-09 14:39:36 test_2[2] start
a: 2
2019-12-09 14:39:36 test_2[2] passed
Upvotes: 2
Views: 630
Reputation: 29977
Your function provider()
yields a single parameter. You have to iterate over the list and yield each item.
def provider():
for param in [1, 2]:
yield pytest.param(param)
Depending on your exact needs, you might be able to simplify that to:
def provider():
yield from [1, 2]
Or even just:
def provider():
return [1, 2]
Upvotes: 1