Skip to content

Commit 543b11f

Browse files
Fix -v hint in raises match diff not working (#14226)
* fix: propagate assertion verbosity to raises match diff The `-v` hint in `pytest.raises(match=...)` diff output was non-functional because `_check_match` called `_diff_text` without passing the verbosity level from the pytest config. Read `VERBOSITY_ASSERTIONS` from the module-level `_config` set during test protocol. Fixes #14214. Co-Authored-By: Claude Opus 4.6 <[email protected]> * Add changelog entry for #14214 Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 <[email protected]>
1 parent 28d9c65 commit 543b11f

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

changelog/14214.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed ``-v`` hint in :func:`pytest.raises` match diff not working because assertion verbosity was not propagated.

src/_pytest/raises.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,19 @@ def _check_match(self, e: BaseException) -> bool:
492492
else ""
493493
)
494494
if isinstance(self.rawmatch, str):
495-
# TODO: it instructs to use `-v` to print leading text, but that doesn't work
496-
# I also don't know if this is the proper entry point, or tool to use at all
495+
from _pytest.assertion.util import _config
497496
from _pytest.assertion.util import _diff_text
498497
from _pytest.assertion.util import dummy_highlighter
498+
from _pytest.config import Config
499499

500-
diff = _diff_text(self.rawmatch, stringified_exception, dummy_highlighter)
500+
verbose = (
501+
_config.get_verbosity(Config.VERBOSITY_ASSERTIONS)
502+
if _config is not None
503+
else 0
504+
)
505+
diff = _diff_text(
506+
self.rawmatch, stringified_exception, dummy_highlighter, verbose
507+
)
501508
self._fail_reason = ("\n" if diff[0][0] == "-" else "") + "\n".join(diff)
502509
return False
503510

testing/python/raises.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,29 @@ def test_consecutive_backslashes_in_escape_check(self) -> None:
487487
assert not is_fully_escaped("\\\\|")
488488
# r"\\\\|" is the string \\\\| (4 backslashes + pipe): even count, pipe is unescaped
489489
assert not is_fully_escaped(r"\\\\|")
490+
491+
def test_raises_match_verbose_diff(self, pytester: Pytester) -> None:
492+
"""Test that -v flag shows full diff in raises match failure (#14214)."""
493+
pytester.makepyfile(
494+
"""
495+
import re
496+
import pytest
497+
498+
def test_raises_v_hint():
499+
prefix = "A" * 60
500+
expected = prefix + " expected_ending"
501+
actual = prefix + " actual_ending"
502+
503+
with pytest.raises(
504+
ValueError, match=f"^{re.escape(expected)}$"
505+
):
506+
raise ValueError(actual)
507+
"""
508+
)
509+
# Without -v: should show "Skipping ... identical leading characters"
510+
result = pytester.runpytest()
511+
result.stdout.fnmatch_lines(["*Skipping*identical leading*"])
512+
513+
# With -v: should NOT show "Skipping" — full diff shown
514+
result_v = pytester.runpytest("-v")
515+
assert "Skipping" not in result_v.stdout.str()

0 commit comments

Comments
 (0)