Skip to content

Commit f92a904

Browse files
committed
feat: add cli, ini option skips pytest internal logic of id generation and raise error
1 parent 36de359 commit f92a904

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

doc/en/reference/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ passed multiple times. The expected format is ``name=value``. For example::
21042104
assert x in (1, 2)
21052105
21062106
will raise an exception due to the duplicate IDs "a".
2107-
when normal pytest behavior would be to handle this by generating unique IDs like "a-0", "a-1".
2107+
Otherwise, pytest would handle this by generating unique IDs like "a0", "a1".
21082108

21092109

21102110
.. _`command-line-flags`:

src/_pytest/python.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def make_unique_parameterset_ids(self) -> list[str | _HiddenParam]:
913913
arugment_values = [
914914
saferepr(param.values) for param in self.parametersets
915915
]
916-
error_msg = f"""
916+
error_msg = textwrap.dedent(f"""
917917
Duplicate parametrization IDs detected, but --require-unique-parametrization-ids is set.
918918
919919
Test name: {self.nodeid}
@@ -924,8 +924,8 @@ def make_unique_parameterset_ids(self) -> list[str | _HiddenParam]:
924924
925925
You can fix this problem using:
926926
the `ids` argument of `@pytest.mark.parametrize()` or the `id` argument of `pytest.param()`.
927-
"""
928-
raise nodes.Collector.CollectError(textwrap.dedent(error_msg))
927+
""")
928+
raise nodes.Collector.CollectError(error_msg)
929929
# Record the number of occurrences of each ID.
930930
id_counts = Counter(resolved_ids)
931931
# Map the ID to its next suffix.

testing/test_collection.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# mypy: allow-untyped-defs
22
from __future__ import annotations
33

4+
from collections.abc import Sequence
45
from collections.abc import Sequence
56
import os
67
from pathlib import Path
@@ -2708,41 +2709,6 @@ def test_1(): pass
27082709

27092710

27102711
class TestRequireUniqueParametrizationtIds:
2711-
@staticmethod
2712-
def _fnmatch_escape_repr(obj: Sequence[tuple[int]]) -> str:
2713-
return re.sub(r"[*?[\]]", (lambda m: f"[{m.group()}]"), repr(obj))
2714-
2715-
def _assert_duplicate_msg(
2716-
self,
2717-
result: RunResult,
2718-
expected_indices: dict[str, Sequence[int]],
2719-
argument_values: Sequence[str],
2720-
resolved_ids: Sequence[str],
2721-
) -> None:
2722-
stream = result.stdout
2723-
stream.fnmatch_lines(
2724-
[
2725-
"Duplicate parametrization IDs detected, but --require-unique-parametrization-ids is set.",
2726-
"Test name: *::test1",
2727-
"Argument names: [[]'x', 'y'[]]",
2728-
f"Argument values: {argument_values}",
2729-
f"Resolved IDs: {resolved_ids}",
2730-
f"Duplicates: {expected_indices}",
2731-
"You can fix this problem using:",
2732-
"the `ids` argument of `@pytest.mark.parametrize()` or the `id` argument of `pytest.param()`.",
2733-
]
2734-
)
2735-
assert result.ret != 0
2736-
2737-
def _convert_to_resolved_ids(
2738-
self, parametrize_args: Sequence[tuple[int, int]]
2739-
) -> Sequence[str]:
2740-
return [f"{x}-{y}" for (x, y) in parametrize_args]
2741-
2742-
def _convert_to_argument_values(
2743-
self, parametrize_args: Sequence[tuple[int, int]]
2744-
) -> Sequence[str]:
2745-
return [str(t) for t in parametrize_args]
27462712

27472713
# Change the test data to native Python types
27482714
CASES = [
@@ -2812,19 +2778,53 @@ def test_cli_overrides_ini_false(self, pytester: Pytester) -> None:
28122778
require_unique_parametrization_ids = false
28132779
"""
28142780
)
2781+
parametrization_args = [(1, 1), (1, 1)]
28152782
pytester.makepyfile(
2816-
"""
2783+
f"""
28172784
import pytest
28182785
2819-
@pytest.mark.parametrize('x, y', [(1,1), (1,1)])
2786+
@pytest.mark.parametrize('x, y', {parametrization_args})
28202787
def test1(y, x):
28212788
pass
28222789
"""
28232790
)
28242791
result = pytester.runpytest("--require-unique-parametrization-ids")
2825-
parametrization_args = [(1, 1), (1, 1)]
28262792
resolved_ids = self._convert_to_resolved_ids(parametrization_args)
28272793
argument_values = self._convert_to_argument_values(parametrization_args)
28282794
self._assert_duplicate_msg(
28292795
result, {"1-1": [0, 1]}, argument_values, resolved_ids
28302796
)
2797+
2798+
def _assert_duplicate_msg(
2799+
self,
2800+
result: RunResult,
2801+
expected_indices: dict[str, Sequence[int]],
2802+
argument_values: Sequence[str],
2803+
resolved_ids: Sequence[str],
2804+
) -> None:
2805+
stream = result.stdout
2806+
stream.fnmatch_lines(
2807+
[
2808+
"Duplicate parametrization IDs detected, but --require-unique-parametrization-ids is set.",
2809+
"Test name: *::test1",
2810+
"Argument names: [[]'x', 'y'[]]",
2811+
f"Argument values: {argument_values}",
2812+
f"Resolved IDs: {resolved_ids}",
2813+
f"Duplicates: {expected_indices}",
2814+
"You can fix this problem using:",
2815+
"the `ids` argument of `@pytest.mark.parametrize()` or the `id` argument of `pytest.param()`.",
2816+
]
2817+
)
2818+
assert result.ret != 0
2819+
2820+
def _convert_to_resolved_ids(
2821+
self, parametrize_args: Sequence[tuple[int, int]]
2822+
) -> Sequence[str]:
2823+
return [
2824+
f"{argument1}-{argument2}" for (argument1, argument2) in parametrize_args
2825+
]
2826+
2827+
def _convert_to_argument_values(
2828+
self, parametrize_args: Sequence[tuple[int, int]]
2829+
) -> Sequence[str]:
2830+
return [str(argument) for argument in parametrize_args]

0 commit comments

Comments
 (0)