Skip to content
Closed
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
5 changes: 5 additions & 0 deletions changelog/13235.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed :func:`pytest.mark.parametrize` with an empty ``argvalues`` list and a
callable ``ids`` parameter leaking the internal ``NOTSET`` string as the test
ID (e.g. ``test_foo[NOTSET]``). The callable is no longer invoked for the
placeholder parameterset, and the generated ID now follows the standard
auto-naming convention (e.g. ``test_foo[x0]``).
4 changes: 1 addition & 3 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ def _for_parametrize(
# parameter set with NOTSET values, with the "empty parameter set" mark applied to it.
mark = get_empty_parameterset_mark(config, argnames, func)
parameters.append(
ParameterSet(
values=(NOTSET,) * len(argnames), marks=[mark], id="NOTSET"
)
ParameterSet(values=(NOTSET,) * len(argnames), marks=[mark], id=None)
)
return argnames, parameters

Expand Down
2 changes: 2 additions & 0 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,8 @@ def _idval_from_function(self, val: object, argname: str, idx: int) -> str | Non
user-provided id callable, if given."""
if self.idfn is None:
return None
if val is NOTSET:
return None
try:
id = self.idfn(val)
except Exception as e:
Expand Down
15 changes: 15 additions & 0 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,21 @@ def test_temp(temp):
result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 1 skipped *"])

def test_parametrize_empty_argvalues_callable_ids(self, pytester: Pytester) -> None:
"""Callable ids with empty argvalues must not crash or leak 'NOTSET' (#13235)."""
pytester.makepyfile(
"""
import pytest

@pytest.mark.parametrize("x", [], ids=lambda x: x.id)
def test_foo(x):
pass
"""
)
result = pytester.runpytest("-v")
result.stdout.no_fnmatch_line("*NOTSET*")
result.stdout.fnmatch_lines(["* 1 skipped *"])

def test_parametrized_ids_invalid_type(self, pytester: Pytester) -> None:
"""Test error with non-strings/non-ints, without generator (#1857)."""
pytester.makepyfile(
Expand Down
Loading