Skip to content Skip to sidebar Skip to footer

Parametrizing Test With Multiple Fixtures That Accept Arguments

I am trying to test a math function that I wrote. I would like to supply data to it from a number of different fixtures. The issue is that all of the fixtures accept different fixt

Solution 1:

Passing fixture as parameters in the parametrization marker is not supported by pytest. See issue #349 for more details: Using fixtures in pytest.mark.parametrize. When in need of parametrizing with fixtures, I usually resort to creating an auxiliary fixture that accepts all the parameter fixtures and then parametrizing that indirectly in the test. Your example would thus become:

@pytest.fixture
def data(request, clean_data, noisy_data):
    type = request.param
    if type == 'clean':
        return clean_data
    elif type == 'noisy':
        return noisy_data
    else:
        raise ValueError('unknown type')

@pytest.mark.parametrize('data', ['clean', 'noisy'], indirect=True)
def test_myfunc(x_data, data):
    shape, y_data = data
    assert myfunc(x_data, y_data)

Post a Comment for "Parametrizing Test With Multiple Fixtures That Accept Arguments"