Skip to content

Commit 4dcbcc9

Browse files
authored
Merge pull request #13458 from pytest-dev/dup-param-error
python: a bit nicer error on duplicate parametrization
2 parents 5293016 + 30407eb commit 4dcbcc9

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

changelog/13457.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The error message about duplicate parametrization no longer displays an internal stack trace.

src/_pytest/python.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,13 +1073,16 @@ def setmulti(
10731073
marks: Iterable[Mark | MarkDecorator],
10741074
scope: Scope,
10751075
param_index: int,
1076+
nodeid: str,
10761077
) -> CallSpec2:
10771078
params = self.params.copy()
10781079
indices = self.indices.copy()
10791080
arg2scope = dict(self._arg2scope)
10801081
for arg, val in zip(argnames, valset):
10811082
if arg in params:
1082-
raise ValueError(f"duplicate parametrization of {arg!r}")
1083+
raise nodes.Collector.CollectError(
1084+
f"{nodeid}: duplicate parametrization of {arg!r}"
1085+
)
10831086
params[arg] = val
10841087
indices[arg] = param_index
10851088
arg2scope[arg] = scope
@@ -1233,6 +1236,8 @@ def parametrize(
12331236
It will also override any fixture-function defined scope, allowing
12341237
to set a dynamic scope using test context or configuration.
12351238
"""
1239+
nodeid = self.definition.nodeid
1240+
12361241
argnames, parametersets = ParameterSet._for_parametrize(
12371242
argnames,
12381243
argvalues,
@@ -1244,7 +1249,7 @@ def parametrize(
12441249

12451250
if "request" in argnames:
12461251
fail(
1247-
"'request' is a reserved name and cannot be used in @pytest.mark.parametrize",
1252+
f"{nodeid}: 'request' is a reserved name and cannot be used in @pytest.mark.parametrize",
12481253
pytrace=False,
12491254
)
12501255

@@ -1339,6 +1344,7 @@ def parametrize(
13391344
marks=param_set.marks,
13401345
scope=scope_,
13411346
param_index=param_index,
1347+
nodeid=nodeid,
13421348
)
13431349
newcalls.append(newcallspec)
13441350
self._calls = newcalls

testing/python/metafunc.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,15 @@ def func(x, y):
8282

8383
metafunc = self.Metafunc(func)
8484
metafunc.parametrize("x", [1, 2])
85-
pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6]))
86-
pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6]))
85+
with pytest.raises(pytest.Collector.CollectError):
86+
metafunc.parametrize("x", [5, 6])
87+
with pytest.raises(pytest.Collector.CollectError):
88+
metafunc.parametrize("x", [5, 6])
8789
metafunc.parametrize("y", [1, 2])
88-
pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6]))
89-
pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6]))
90+
with pytest.raises(pytest.Collector.CollectError):
91+
metafunc.parametrize("y", [5, 6])
92+
with pytest.raises(pytest.Collector.CollectError):
93+
metafunc.parametrize("y", [5, 6])
9094

9195
with pytest.raises(TypeError, match="^ids must be a callable or an iterable$"):
9296
metafunc.parametrize("y", [5, 6], ids=42) # type: ignore[arg-type]

0 commit comments

Comments
 (0)