I have created a 'dynamic' list of parameters which I pass to parametrize.
OPTIONS = ['a', 'b', 'c']
def get_unique_pairs():
unique_list = []
for first in OPTIONS:
for second OPTIONS:
if first == second:
continue
unique_list.append({'first':first, 'second':second))
return unique_list
def some_func()
unique_pairs = get_unique_pairs()
result = []
for pair in unique_pair:
if test(pair):
continue
else:
result.append(pair)
return pair
@pytest.mark.parametrize('param', some_fnc())
def test_fnc(param):
first = param['first']
second = param['second']
The input I wish to pass to test_fnc is [('a','b'),('a','c')...('c','b')] where the first and second elements are never the same. There is some additional logic I am using to further remove specific pairs.
When I run the test I get the output:
::test_fnc[param0] PASSED
::test_fnc[param1] PASSED
::test_fnc[param2] PASSED
I have two issues:
- I'm not entirely sure how to describe what I am doing to find further documentation / help.
- I would like more descriptive output (i.e. not
param0), and I'd like to continue using dictionaries to pass data to the test.
get_unique_pairscan be replaced bylist(itertools.combinations(OPTIONS, 2)). Also, take a look at generator functions and generator expressions, they can make your code more concise and idiomatic.combinations(p, r)iterator returns r-length tuples in sorted order, no repeated elements. Product is equivalent to your nested for loop:[(a, b) for a, b in product(OPTIONS, OPTIONS) if a != b]itertools.permutations(OPTIONS, 2)is a much cleaner way of writing what I was doing!