Just wondering, is there any (more) elegant way of parameterizing with the cartesian product? This is what I figured out so far:
numbers = [1,2,3,4,5]
vow
I think besides an elegant solution, you should also consider both the amount of time each option will take and the amount of code you'll have to maintain.
parametrize once with itertools (provided by Frank T)parametrize 3 times (provided by Bruno Oliveira)@pytest.mark.parametrize('number, vowel, consonant',
itertools.product(numbers, vowels, consonants))
def test(number, vowel, consonant):
pass
@pytest.fixture(params=numbers)
def number(request): return request.param
@pytest.fixture(params=vowels)
def vowel(request): return request.param
@pytest.fixture(params=consonants)
def consonant(request): return request.param
def test(number, vowel, consonant):
pass
@pytest.mark.parametrize('number', numbers)
@pytest.mark.parametrize('vowel', vowels)
@pytest.mark.parametrize('consonant', consonants)
def test(number, vowel, consonant):
pass
@pytest.fixture(params=cartesian)
def someparams(request):
return request.param
def test_something(someparams):
pass
When it comes to elegance, I consider that Solution 3 is the best option because it has less code maintain, and it does not require to import itertools. After that Solution 1 is the best choice because you don't need to write fixtures as Solution 4, and Solution 2. Solution 4 is probably better than Solution 2 because it requires less code to maintain.
When it comes to performance I run each solution using numbers = list(range(100)), and I got the following results:
| Solution | Time |
| Solution 1 | 3.91s |
| Solution 2 | 3.59s |
| Solution 3 | 3.54s |
| Solution 4 | 3.09s |