From 7156a97f9a1471f35c48988f2153047db4fd5e95 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 27 Oct 2023 17:23:57 -0300 Subject: [PATCH] Fix crash using empty string for parametrized value more than once Fixes #11563. --- changelog/11563.bugfix.rst | 1 + src/_pytest/python.py | 2 +- testing/python/metafunc.py | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 changelog/11563.bugfix.rst diff --git a/changelog/11563.bugfix.rst b/changelog/11563.bugfix.rst new file mode 100644 index 000000000..35b5e4f15 --- /dev/null +++ b/changelog/11563.bugfix.rst @@ -0,0 +1 @@ +Fixed crash when using an empty string for the same parametrized value more than once. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index f54bbb379..0985c871d 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -1003,7 +1003,7 @@ class IdMaker: for index, id in enumerate(resolved_ids): if id_counts[id] > 1: suffix = "" - if id[-1].isdigit(): + if id and id[-1].isdigit(): suffix = "_" new_id = f"{id}{suffix}{id_suffixes[id]}" while new_id in set(resolved_ids): diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 16ec37f9a..e93363a78 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -626,6 +626,13 @@ class TestMetafunc: ).make_unique_parameterset_ids() assert result == [expected] + def test_idmaker_duplicated_empty_str(self) -> None: + """Regression test for empty strings parametrized more than once (#11563).""" + result = IdMaker( + ("a",), [pytest.param(""), pytest.param("")], None, None, None, None, None + ).make_unique_parameterset_ids() + assert result == ["0", "1"] + def test_parametrize_ids_exception(self, pytester: Pytester) -> None: """ :param pytester: the instance of Pytester class, a temporary