Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ Mihail Milushev
Mike Fiedler (miketheman)
Mike Hoyle (hoylemd)
Mike Lundy
Mike Ma
Milan Lesnek
minbang930
Miro Hrončok
Expand Down
1 change: 1 addition & 0 deletions changelog/14619.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a crash in :func:`pytest.mark.parametrize` when a non-sequence value is passed for a parameter set. It now provides a clear error message during collection.
14 changes: 12 additions & 2 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,17 @@ def _for_parametrize(
if parameters:
# Check all parameter sets have the correct number of values.
for param in parameters:
if len(param.values) != len(argnames):
try:
values_len = len(param.values)
except TypeError:
values_len = None
if values_len is None:
fail(
f'{nodeid}: in "parametrize" expected a sequence of values, got {type(param.values).__name__}:\n'
f" {param.values}",
pytrace=False,
)
if values_len != len(argnames):
msg = (
'{nodeid}: in "parametrize" the number of names ({names_len}):\n'
" {names}\n"
Expand All @@ -227,7 +237,7 @@ def _for_parametrize(
values=param.values,
names=argnames,
names_len=len(argnames),
values_len=len(param.values),
values_len=values_len,
),
pytrace=False,
)
Expand Down
22 changes: 22 additions & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,28 @@ def test_func(foo, bar):
)


def test_parametrized_collect_with_non_sequence_values(pytester: Pytester) -> None:
"""Test collect parametrized func with tuple-style argnames and scalar values."""
py_file = pytester.makepyfile(
"""
import pytest

@pytest.mark.parametrize("x,", [None])
def test_func(x):
pass
"""
)

result = pytester.runpytest(py_file)
result.stdout.fnmatch_lines(
[
"test_parametrized_collect_with_non_sequence_values.py::test_func: "
'in "parametrize" expected a sequence of values, got NoneType:',
" None",
]
)


def test_parametrized_with_kwargs(pytester: Pytester) -> None:
"""Test collect parametrized func with wrong number of args."""
py_file = pytester.makepyfile(
Expand Down
Loading