0

Imagine the following testsuite

import pytest

@pytest.fixture(params=1, 2, 3)
def shape(request):
    return request.param


@pytest.fixture
def data(shape):
    return shape


def test_resize(data, shape):
    pass

where I have two fixtures data and shape. data depends on the fixture shape and is being generated for each of the possible values. But in test_resize I want to test over all possible combinations of data and shape:

  • 1, 1
  • 1, 2
  • 1, 3
  • 2, 1

etc. With the implementation above it does not expand the carthesian product though:

  • 1, 1
  • 2, 2
  • 3, 3

Is there a way to make py.test expand the fixtures to all possible combinations?

Nils Werner
  • 34,832
  • 7
  • 76
  • 98

1 Answers1

2

As your output shows, shape is parameterized but data is not, thus there will only be one instance of the data fixture for each instance of the shape fixture. I would also parameterize data. Then, still having the data fixture depend upon shape, you'll get the product you desire:

import pytest

fixture_params = (1, 2, 3)

@pytest.fixture(params=fixture_params)
def shape(request):
    return request.param

@pytest.fixture(params=fixture_params)
def data(request, shape):
    print(request.param)
    return request.param

def test_resize(data, shape):
    print(data, shape)
    assert 0 and 'assert to show prints'
random8
  • 81
  • 1
  • 5