1

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
Diana
  • 86
  • 6

1 Answers1

1

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]
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • Thank you! And do you know if there is any way to pass parameter (that is a custom fixture) to provider method? I mean on pytest decorator side – Diana Dec 10 '19 at 12:38