Skip to content

Commit e15da3b

Browse files
author
Sylvain MARIE
committed
Fixed issues when parametrize argnames contains a list. Fixed #49
Added corresponding test. New util method `get_param_argnames_as_list` used everywhere.
1 parent af50566 commit e15da3b

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

pytest_cases/common.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ def get_fixture_name(fixture_fun):
4646
return str(fixture_fun)
4747

4848

49+
def get_param_argnames_as_list(argnames):
50+
"""
51+
pytest parametrize accepts both coma-separated names and list/tuples.
52+
This function makes sure that we always return a list
53+
:param argnames:
54+
:return:
55+
"""
56+
if isinstance(argnames, str):
57+
argnames = argnames.replace(' ', '').split(',')
58+
return list(argnames)
59+
60+
4961
# ------------ container for the mark information that we grab from the fixtures (`@pytest_fixture_plus`)
5062
class _ParametrizationMark:
5163
"""
@@ -55,7 +67,7 @@ class _ParametrizationMark:
5567

5668
def __init__(self, mark):
5769
bound = get_parametrize_signature().bind(*mark.args, **mark.kwargs)
58-
self.param_names = bound.arguments['argnames'].replace(' ', '').split(',')
70+
self.param_names = get_param_argnames_as_list(bound.arguments['argnames'])
5971
self.param_values = bound.arguments['argvalues']
6072
try:
6173
bound.apply_defaults()
@@ -107,7 +119,7 @@ def get_param_names(fnode):
107119
p_markers = get_parametrization_markers(fnode)
108120
param_names = []
109121
for paramz_mark in p_markers:
110-
param_names += paramz_mark.args[0].replace(' ', '').split(',')
122+
param_names += get_param_argnames_as_list(paramz_mark.args[0])
111123
return param_names
112124

113125

pytest_cases/main_fixtures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
pass
4040

4141
from pytest_cases.common import yield_fixture, get_pytest_parametrize_marks, get_test_ids_from_param_values, \
42-
make_marked_parameter_value, extract_parameterset_info, get_fixture_name
42+
make_marked_parameter_value, extract_parameterset_info, get_fixture_name, get_param_argnames_as_list
4343
from pytest_cases.main_params import cases_data
4444

4545

@@ -167,7 +167,7 @@ def param_fixtures(argnames, argvalues, autouse=False, ids=None, scope="function
167167
:return:
168168
"""
169169
created_fixtures = []
170-
argnames_lst = argnames.replace(' ', '').split(',')
170+
argnames_lst = get_param_argnames_as_list(argnames)
171171

172172
caller_module = get_caller_module()
173173

@@ -763,7 +763,7 @@ def pytest_parametrize_plus(argnames, argvalues, indirect=False, ids=None, scope
763763
else:
764764
# there are fixture references: we have to create a specific decorator
765765
caller_module = get_caller_module()
766-
all_param_names = argnames.replace(' ', '').split(',')
766+
all_param_names = get_param_argnames_as_list(argnames)
767767

768768
def create_param_fixture(from_i, to_i, p_fix_name):
769769
""" Routine that will be used to create a parameter fixture for argvalues between prev_i and i"""

pytest_cases/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
from pytest_cases.common import get_pytest_nodeid, get_pytest_function_scopenum, \
11-
is_function_node, get_param_names, get_pytest_scopenum
11+
is_function_node, get_param_names, get_pytest_scopenum, get_param_argnames_as_list
1212
from pytest_cases.main_fixtures import NOT_USED, is_fixture_union_params, UnionFixtureAlternative, apply_id_style
1313

1414
try: # python 3.3+
@@ -708,7 +708,7 @@ def create_call_list_from_pending_parametrizations(self):
708708

709709
# ------ parametrize the calls --------
710710
# create a dictionary of pending things to parametrize, and only keep the first parameter in case of several
711-
pending_items = [(str(p[0]).replace(' ', '').split(',')[0], p) for p in self._pending]
711+
pending_items = [(get_param_argnames_as_list(p[0])[0], p) for p in self._pending]
712712
pending = OrderedDict(pending_items)
713713

714714
if _DEBUG:
@@ -766,7 +766,7 @@ def _cleanup_calls_list(self, fix_closure_tree, calls, nodes, pending):
766766
indirect=True, discard_id=True,
767767
scope=p_to_apply.scope, **p_to_apply.kwargs)
768768
else:
769-
_nb_argnames = len(p_to_apply.argnames.split(','))
769+
_nb_argnames = len(get_param_argnames_as_list(p_to_apply.argnames))
770770
if _nb_argnames > 1:
771771
_vals = [(NOT_USED,) * _nb_argnames]
772772
else:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
from pytest_cases import pytest_fixture_plus
3+
4+
5+
@pytest_fixture_plus
6+
@pytest.mark.parametrize(['c', 'd'], [(0, 0), (1, 1)])
7+
def f(c, d):
8+
pass
9+
10+
11+
@pytest.mark.parametrize(['a', 'b'], [(0, 0), (1, 1)])
12+
def test_dummy(a, b, f):
13+
pass
14+
15+
16+
def test_synthesis(module_results_dct):
17+
assert list(module_results_dct) == ["test_dummy[0-0-0-0]",
18+
"test_dummy[0-0-1-1]",
19+
"test_dummy[1-1-0-0]",
20+
"test_dummy[1-1-1-1]"]

0 commit comments

Comments
 (0)