Repository: pytest-dev/pytest
File: testing/test_collection.py (Python)

Code before:
            "<Dir pkg>",
            "  <Dir subpkg_namespace>",
            "    <Module module2.py>",
            "      <Function test_module1>",
        ]
    )

    result = pytester.runpytest("--collect-only", "--pyargs", "pkg.subpkg_regular")
    result.stdout.fnmatch_lines(
        [
            "collected 1 item",
            "<Dir pkg>",
            "  <Package subpkg_regular>",
            "    <Module module3.py>",
            "      <Function test_module3>",
        ]
    )


@pytest.mark.parametrize(
    "parametrize_args, expected_indexs",
    [
        ("[(1, 1), (1, 1)]", "{'1-1': [[]0, 1[]]}"),
        ("[(1, 1), (1, 2), (1, 1)]", "{'1-1': [[]0, 2[]]}"),
        ("[(1, 1), (2, 2), (1, 1)]", "{'1-1': [[]0, 2[]]}"),
        ("[(1, 1), (2, 2), (1, 2), (2, 1), (1, 1)]", "{'1-1': [[]0, 4[]]}"),
    ],
)
def test_option_parametrize_require_unique_paramset_ids(
    pytester: Pytester, parametrize_args, expected_indexs
) -> None:
    pytester.makepyfile(
        f"""
        import pytest
        @pytest.mark.parametrize('y, x', {parametrize_args})
        def test1(y, x):
            pass
    """
    )
    result = pytester.runpytest("--require-unique-paramset-ids")
    result.stdout.fnmatch_lines(
        [
            "E*Because: require_unique_parameterset_ids is set, pytest won't",
            "E*attempt to generate unique IDs for parameter sets.",
            "E*argument names: [[]'y', 'x'[]]",
            "E*function name: test1",
            "E*test name: test_option_parametrize_require_unique_paramset_ids.py::test1",
            f"E*duplicates: {expected_indexs!s}",
        ]
    )


Review comment (security):
I find the argument names and `expected_indexs` hard to read; how about we define a helper function:

```python
def fnmatch_escape_repr(obj):
    return re.sub(r"[*?[\]]", (lambda m: "[{}]".format(m.group())), repr(obj))
```

Code after:
        [
            "collected 1 item",
            "<Dir pkg>",
            "  <Dir subpkg_namespace>",
            "    <Module module2.py>",
            "      <Function test_module1>",
        ]
    )

    result = pytester.runpytest("--collect-only", "--pyargs", "pkg.subpkg_regular")
    result.stdout.fnmatch_lines(
        [
            "collected 1 item",
            "<Dir pkg>",
            "  <Package subpkg_regular>",
            "    <Module module3.py>",
            "      <Function test_module3>",
        ]
    )


class TestOverlappingCollectionArguments:
    """Test that overlapping collection arguments (e.g. `pytest a/b a
    a/c::TestIt) are handled correctly (#12083)."""

    @pytest.mark.parametrize("args", [("a", "a/b"), ("a/b", "a")])
    def test_parent_child(self, pytester: Pytester, args: tuple[str, ...]) -> None:
        """Test that 'pytest a a/b' and `pytest a/b a` collects all tests from 'a'."""
        pytester.makepyfile(
            **{
                "a/test_a.py": """
                    def test_a1(): pass
                    def test_a2(): pass
                """,
                "a/b/test_b.py": """
                    def test_b1(): pass
                    def test_b2(): pass
                """,
            }
        )

        result = pytester.runpytest("--collect-only", *args)

        result.stdout.fnmatch_lines(
            [
                "<Dir *>",
                "  <Dir a>",
                "    <Dir b>",
                "      <Module test_b.py>",
                "        <Function test_b1>",
                "        <Function test_b2>",