From c762d5a7a7c87bf035aabfec9642d555c9dec7e7 Mon Sep 17 00:00:00 2001 From: Rowan Stein Date: Thu, 25 Dec 2025 20:44:48 +0000 Subject: [PATCH] Fix: preserve '.[' in long report headlines by returning modpath unchanged; add regression test for param id with '..[' (#22) --- src/_pytest/python.py | 2 +- testing/test_headline_modpath_param_id.py | 24 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 testing/test_headline_modpath_param_id.py diff --git a/src/_pytest/python.py b/src/_pytest/python.py index b8b365ad34d..5f2af31e20a 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -286,7 +286,7 @@ def getmodpath(self, stopatmodule=True, includemodule=False): parts.append(name) parts.reverse() s = ".".join(parts) - return s.replace(".[", "[") + return s def reportinfo(self): # XXX caching? diff --git a/testing/test_headline_modpath_param_id.py b/testing/test_headline_modpath_param_id.py new file mode 100644 index 00000000000..6f8b30a6bd3 --- /dev/null +++ b/testing/test_headline_modpath_param_id.py @@ -0,0 +1,24 @@ +import pytest + + +def test_headline_preserves_dot_bracket_in_param_id(testdir): + testdir.makepyfile( + **{ + "test_sample.py": ( + """ + import pytest + + @pytest.mark.parametrize("arg", [0], ids=["a..[b]"]) + def test_boo(arg): + assert False + """ + ) + } + ) + result = testdir.runpytest("-vv") + # Ensure the failure headline contains the exact param id text without mutation + result.stdout.fnmatch_lines([ + "*FAILURES*", + "*test_boo[a..[b]]*", + ]) + assert result.ret != 0