Skip to content

Commit

Permalink
Fixed issues when parametrize argnames contains a list. Fixed #49
Browse files Browse the repository at this point in the history
Added corresponding test.
New util method `get_param_argnames_as_list` used everywhere.
  • Loading branch information
Sylvain MARIE committed Jun 27, 2019
1 parent af50566 commit e15da3b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
16 changes: 14 additions & 2 deletions pytest_cases/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ def get_fixture_name(fixture_fun):
return str(fixture_fun)


def get_param_argnames_as_list(argnames):
"""
pytest parametrize accepts both coma-separated names and list/tuples.
This function makes sure that we always return a list
:param argnames:
:return:
"""
if isinstance(argnames, str):
argnames = argnames.replace(' ', '').split(',')
return list(argnames)


# ------------ container for the mark information that we grab from the fixtures (`@pytest_fixture_plus`)
class _ParametrizationMark:
"""
Expand All @@ -55,7 +67,7 @@ class _ParametrizationMark:

def __init__(self, mark):
bound = get_parametrize_signature().bind(*mark.args, **mark.kwargs)
self.param_names = bound.arguments['argnames'].replace(' ', '').split(',')
self.param_names = get_param_argnames_as_list(bound.arguments['argnames'])
self.param_values = bound.arguments['argvalues']
try:
bound.apply_defaults()
Expand Down Expand Up @@ -107,7 +119,7 @@ def get_param_names(fnode):
p_markers = get_parametrization_markers(fnode)
param_names = []
for paramz_mark in p_markers:
param_names += paramz_mark.args[0].replace(' ', '').split(',')
param_names += get_param_argnames_as_list(paramz_mark.args[0])
return param_names


Expand Down
6 changes: 3 additions & 3 deletions pytest_cases/main_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
pass

from pytest_cases.common import yield_fixture, get_pytest_parametrize_marks, get_test_ids_from_param_values, \
make_marked_parameter_value, extract_parameterset_info, get_fixture_name
make_marked_parameter_value, extract_parameterset_info, get_fixture_name, get_param_argnames_as_list
from pytest_cases.main_params import cases_data


Expand Down Expand Up @@ -167,7 +167,7 @@ def param_fixtures(argnames, argvalues, autouse=False, ids=None, scope="function
:return:
"""
created_fixtures = []
argnames_lst = argnames.replace(' ', '').split(',')
argnames_lst = get_param_argnames_as_list(argnames)

caller_module = get_caller_module()

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

def create_param_fixture(from_i, to_i, p_fix_name):
""" Routine that will be used to create a parameter fixture for argvalues between prev_i and i"""
Expand Down
6 changes: 3 additions & 3 deletions pytest_cases/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

from pytest_cases.common import get_pytest_nodeid, get_pytest_function_scopenum, \
is_function_node, get_param_names, get_pytest_scopenum
is_function_node, get_param_names, get_pytest_scopenum, get_param_argnames_as_list
from pytest_cases.main_fixtures import NOT_USED, is_fixture_union_params, UnionFixtureAlternative, apply_id_style

try: # python 3.3+
Expand Down Expand Up @@ -708,7 +708,7 @@ def create_call_list_from_pending_parametrizations(self):

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

if _DEBUG:
Expand Down Expand Up @@ -766,7 +766,7 @@ def _cleanup_calls_list(self, fix_closure_tree, calls, nodes, pending):
indirect=True, discard_id=True,
scope=p_to_apply.scope, **p_to_apply.kwargs)
else:
_nb_argnames = len(p_to_apply.argnames.split(','))
_nb_argnames = len(get_param_argnames_as_list(p_to_apply.argnames))
if _nb_argnames > 1:
_vals = [(NOT_USED,) * _nb_argnames]
else:
Expand Down
20 changes: 20 additions & 0 deletions pytest_cases/tests/issues/test_parametrize_with_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
from pytest_cases import pytest_fixture_plus


@pytest_fixture_plus
@pytest.mark.parametrize(['c', 'd'], [(0, 0), (1, 1)])
def f(c, d):
pass


@pytest.mark.parametrize(['a', 'b'], [(0, 0), (1, 1)])
def test_dummy(a, b, f):
pass


def test_synthesis(module_results_dct):
assert list(module_results_dct) == ["test_dummy[0-0-0-0]",
"test_dummy[0-0-1-1]",
"test_dummy[1-1-0-0]",
"test_dummy[1-1-1-1]"]

0 comments on commit e15da3b

Please sign in to comment.