The inputs to the the algorithm being tested is a list of strings (known in advance) with associated years, e.g. all of the following are valid inputs:
['A_2018', 'B_2019', 'C_2018']
[]
['A_2018']
I've got the following simple year fixture:
@pytest.fixture(params=range(2018, 2025))
def year(request):
return request.param
If I create individual fixtures for each valid string:
@pytest.fixture(params=['A', ''])
def A(request, year):
return request.param, year
@pytest.fixture(params=['B', ''])
def B(request, year):
return request.param, year
etc. and use them in tests like:
def test_foo(A, B):
args = []
if A[0]:
args.append('%s_%d' % A)
if B[0]:
args.append('%s_%d' % B)
assert old_algorithm(args) == new_algorithm(args)
I get
['A_2018', 'B_2018']
['A_2019', 'B_2019']
['A_2020', 'B_2020']
etc. where the year is always the same for both parameters.
Is there a way to create all combinations?
Is a fixture generating combinations a requirement? Because otherwise, applying pytest.mark.parametrize
separately for each test input arg generates the input args combinations just fine. In the example below both fixtures A
and B
are parametrized separately, generating (2025 - 2018)**2
tests in total:
@pytest.fixture
def A(request):
return 'A', request.param
@pytest.fixture
def B(request):
return 'B', request.param
@pytest.mark.parametrize('A', range(2018, 2025), indirect=True, ids=lambda year: 'A({})'.format(year))
@pytest.mark.parametrize('B', range(2018, 2025), indirect=True, ids=lambda year: 'B({})'.format(year))
def test_foo(A, B):
assert A[0] == 'A' and B[0] == 'B'
As a result, 49 tests are produced:
$ pytest --collect-only | grep collected
collected 49 items
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With