Skip to content

Commit d4f6e17

Browse files
committed
feat: add markers support in idf_parametrize
1 parent 33f3bed commit d4f6e17

File tree

1 file changed

+27
-20
lines changed
  • pytest-embedded-idf/pytest_embedded_idf

1 file changed

+27
-20
lines changed

pytest-embedded-idf/pytest_embedded_idf/utils.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,44 @@ def _expand_target_values(values: t.List[t.List[t.Any]], target_index: int) -> t
2424
return expanded_values
2525

2626

27-
def _process_pytest_value(value: t.Union[t.List[t.Any], t.Any], markers_index: int) -> t.Any:
27+
def _process_pytest_value(value: t.Union[t.List[t.Any], t.Any], param_count: int) -> t.Any:
2828
"""
2929
Processes a single parameter value, converting it to pytest.param if needed.
3030
"""
3131
if not isinstance(value, (list, tuple)):
3232
return value
3333

34+
if len(value) > param_count + 1:
35+
raise ValueError(f'Expected at most {param_count + 1} elements (params + marks), got {len(value)}')
36+
3437
params, marks = [], []
35-
for i, element in enumerate(value):
36-
if i == markers_index:
37-
if not isinstance(element, tuple):
38-
element = (element,)
39-
if isinstance(element, tuple):
40-
marks.extend(element)
41-
else:
42-
params.append(element)
38+
if len(value) > param_count:
39+
mark_values = value[-1]
40+
marks.extend(mark_values if isinstance(mark_values, (tuple, list)) else (mark_values,))
41+
42+
params.extend(value[:param_count])
4343

4444
return pytest.param(*params, marks=tuple(marks))
4545

4646

4747
def idf_parametrize(
48-
param_names: str, values: t.List[t.Union[t.Any, t.Tuple[t.Any, ...]]], indirect: bool = False
48+
param_names: str,
49+
values: t.List[t.Union[t.Any, t.Tuple[t.Any, ...]]],
50+
indirect: (t.Union[bool, t.Sequence[str]]) = False,
4951
) -> t.Callable[..., None]:
5052
"""
5153
A decorator to unify pytest.mark.parametrize usage in esp-idf.
5254
5355
Args:
54-
param_names (str): Comma-separated parameter names.
55-
values (list): List of parameter values. Each value can be a string or a tuple.
56-
indirect (bool): If True, marks parameters as indirect.
56+
param_names: A comma-separated string of parameter names that will be passed to
57+
the test function.
58+
values: A list of parameter values where each value corresponds to the parameters
59+
defined in param_names.
60+
indirect: A list of arguments names (subset of argnames) or a boolean. If True
61+
the list contains all names from the argnames. Each argvalue corresponding to an
62+
argname in this list will be passed as request.param to its respective argname
63+
fixture function so that it can perform more expensive setups during the setup
64+
phase of a test rather than at collection time.
5765
5866
Returns:
5967
Decorated test function with parametrization applied
@@ -63,19 +71,18 @@ def idf_parametrize(
6371
if not param:
6472
raise ValueError(f'One of the provided parameters name is empty: {param_list}')
6573

66-
markers_index = param_list.index('markers') if 'markers' in param_list else -1
74+
param_count = len(param_list)
75+
param_list[:] = [_p for _p in param_list if _p not in ('markers',)]
6776
target_index = param_list.index('target') if 'target' in param_list else -1
68-
69-
filtered_params = [name for name in param_list if name != 'markers']
70-
71-
normalized_values = [[value] if len(param_list) == 1 else list(value) for value in values]
77+
normalized_values = [[value] if param_count == 1 else list(value) for value in values]
78+
param_count = len(param_list)
7279

7380
if target_index != -1:
7481
normalized_values = _expand_target_values(normalized_values, target_index)
7582

76-
processed_values = [_process_pytest_value(value, markers_index) for value in normalized_values]
83+
processed_values = [_process_pytest_value(value, param_count) for value in normalized_values]
7784

7885
def decorator(func):
79-
return pytest.mark.parametrize(','.join(filtered_params), processed_values, indirect=indirect)(func)
86+
return pytest.mark.parametrize(','.join(param_list), processed_values, indirect=indirect)(func)
8087

8188
return decorator

0 commit comments

Comments
 (0)