Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parameterized test with cartesian product of arguments in pytest

Tags:

python

pytest

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] vowels     = ['a','e','i','o','u'] consonants = ['x','y','z']  cartesian = [elem for elem in itertools.product(*[numbers,vowels,consonants])]  @pytest.fixture(params=cartesian) def someparams(request):   return request.param  def test_something(someparams):   pass 

At least I'd like to encapsulate numbers, vowels, consonants and cartesian in the fixture function.

like image 638
memecs Avatar asked Mar 04 '14 12:03

memecs


Video Answer


1 Answers

You can apply multiple parametrize arguments, in which case they will generate a product of all parameters:

import pytest  numbers = [1,2,3,4,5] vowels = ['a','e','i','o','u'] consonants = ['x','y','z']   @pytest.mark.parametrize('number', numbers) @pytest.mark.parametrize('vowel', vowels) @pytest.mark.parametrize('consonant', consonants) def test(number, vowel, consonant):     pass 
like image 62
Bruno Oliveira Avatar answered Sep 19 '22 15:09

Bruno Oliveira