|
5 | 5 | from pathlib import Path
|
6 | 6 | from pathlib import PurePath
|
7 | 7 | import pprint
|
| 8 | +import re |
8 | 9 | import shutil
|
9 | 10 | import sys
|
10 | 11 | import tempfile
|
@@ -2702,3 +2703,73 @@ def test_1(): pass
|
2702 | 2703 | ],
|
2703 | 2704 | consecutive=True,
|
2704 | 2705 | )
|
| 2706 | + |
| 2707 | + |
| 2708 | +class TestRequireUniqueParamsetIds: |
| 2709 | + CASES = [ |
| 2710 | + ("[(1, 1), (1, 1)]", {"1-1": [0, 1]}), |
| 2711 | + ("[(1, 1), (1, 2), (1, 1)]", {"1-1": [0, 2]}), |
| 2712 | + ("[(1, 1), (2, 2), (1, 1)]", {"1-1": [0, 2]}), |
| 2713 | + ("[(1, 1), (2, 2), (1, 2), (2, 1), (1, 1)]", {"1-1": [0, 4]}), |
| 2714 | + ] |
| 2715 | + |
| 2716 | + @staticmethod |
| 2717 | + def _make_testfile(pytester: Pytester, parametrize_args: str) -> None: |
| 2718 | + pytester.makepyfile( |
| 2719 | + f""" |
| 2720 | + import pytest |
| 2721 | +
|
| 2722 | + @pytest.mark.parametrize('y, x', {parametrize_args}) |
| 2723 | + def test1(y, x): |
| 2724 | + pass |
| 2725 | + """ |
| 2726 | + ) |
| 2727 | + |
| 2728 | + @staticmethod |
| 2729 | + def _fnmatch_escape_repr(obj) -> str: |
| 2730 | + return re.sub(r"[*?[\]]", (lambda m: f"[{m.group()}]"), repr(obj)) |
| 2731 | + |
| 2732 | + def _assert_duplicate_msg(self, result, expected_indices): |
| 2733 | + # Collection errors usually go to stdout; fall back to stderr just in case. |
| 2734 | + stream = result.stdout |
| 2735 | + stream.fnmatch_lines( |
| 2736 | + [ |
| 2737 | + "E*Because --require-unique-paramset-ids given, pytest won't", |
| 2738 | + "E*attempt to generate unique IDs for parameter sets.", |
| 2739 | + "E*argument names: [[]'y', 'x'[]]", |
| 2740 | + "E*function name: test1", |
| 2741 | + "E*test name: *::test1", |
| 2742 | + f"E*duplicates: {self._fnmatch_escape_repr(expected_indices)}", |
| 2743 | + ] |
| 2744 | + ) |
| 2745 | + assert result.ret != 0 |
| 2746 | + |
| 2747 | + @pytest.mark.parametrize("parametrize_args, expected_indices", CASES) |
| 2748 | + def test_cli_enables(self, pytester: Pytester, parametrize_args, expected_indices): |
| 2749 | + self._make_testfile(pytester, parametrize_args) |
| 2750 | + result = pytester.runpytest("--require-unique-paramset-ids") |
| 2751 | + self._assert_duplicate_msg(result, expected_indices) |
| 2752 | + |
| 2753 | + @pytest.mark.parametrize("parametrize_args, expected_indices", CASES) |
| 2754 | + def test_ini_enables(self, pytester: Pytester, parametrize_args, expected_indices): |
| 2755 | + pytester.makeini( |
| 2756 | + """ |
| 2757 | + [pytest] |
| 2758 | + require_unique_paramset_ids = true |
| 2759 | + """ |
| 2760 | + ) |
| 2761 | + self._make_testfile(pytester, parametrize_args) |
| 2762 | + result = pytester.runpytest() |
| 2763 | + self._assert_duplicate_msg(result, expected_indices) |
| 2764 | + |
| 2765 | + def test_cli_overrides_ini_false(self, pytester: Pytester): |
| 2766 | + """CLI True should override ini False.""" |
| 2767 | + pytester.makeini( |
| 2768 | + """ |
| 2769 | + [pytest] |
| 2770 | + require_unique_paramset_ids = false |
| 2771 | + """ |
| 2772 | + ) |
| 2773 | + self._make_testfile(pytester, "[(1,1), (1,1)]") |
| 2774 | + result = pytester.runpytest("--require-unique-paramset-ids") |
| 2775 | + self._assert_duplicate_msg(result, {"1-1": [0, 1]}) |
0 commit comments