How to make the second parameterized variable in pytest a function of the first?

24 views Asked by At

I have a pytest test that looks like this:

@pytest.mark.parametrize('type', ['a', 'b', 'c'])
@pytest.mark.parametrize('val', list(range(0, VAR_THAT_IS_FUNCTION_OF_TYPE)))
def test(type, val):

Where the the upper end of the range is VAR_THAT_IS_FUNCTION_OF_TYPE which is a function of type. However, the problem is that type isn't known to the second parameterization call. Is there a way to make it known?

1

There are 1 answers

0
Guy On

You can use a function as parametrize data

def get_params():
    for t in ['a', 'b', 'c']:
        for v in range(function_of_type(t)):
            yield t, v


@pytest.mark.parametrize('t, v', get_params())
def test(t, v):
    print(t, v)