Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/14214.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``-v`` hint in :func:`pytest.raises` match diff not working because assertion verbosity was not propagated.
13 changes: 10 additions & 3 deletions src/_pytest/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,19 @@ def _check_match(self, e: BaseException) -> bool:
else ""
)
if isinstance(self.rawmatch, str):
# TODO: it instructs to use `-v` to print leading text, but that doesn't work
# I also don't know if this is the proper entry point, or tool to use at all
from _pytest.assertion.util import _config
from _pytest.assertion.util import _diff_text
from _pytest.assertion.util import dummy_highlighter
from _pytest.config import Config

diff = _diff_text(self.rawmatch, stringified_exception, dummy_highlighter)
verbose = (
_config.get_verbosity(Config.VERBOSITY_ASSERTIONS)
if _config is not None
else 0
)
diff = _diff_text(
self.rawmatch, stringified_exception, dummy_highlighter, verbose
)
self._fail_reason = ("\n" if diff[0][0] == "-" else "") + "\n".join(diff)
return False

Expand Down
26 changes: 26 additions & 0 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,29 @@ def test_raises_match_compiled_regex(self) -> None:
pattern_with_flags = re.compile(r"INVALID LITERAL", re.IGNORECASE)
with pytest.raises(ValueError, match=pattern_with_flags):
int("asdf")

def test_raises_match_verbose_diff(self, pytester: Pytester) -> None:
"""Test that -v flag shows full diff in raises match failure (#14214)."""
pytester.makepyfile(
"""
import re
import pytest

def test_raises_v_hint():
prefix = "A" * 60
expected = prefix + " expected_ending"
actual = prefix + " actual_ending"

with pytest.raises(
ValueError, match=f"^{re.escape(expected)}$"
):
raise ValueError(actual)
"""
)
# Without -v: should show "Skipping ... identical leading characters"
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*Skipping*identical leading*"])

# With -v: should NOT show "Skipping" — full diff shown
result_v = pytester.runpytest("-v")
assert "Skipping" not in result_v.stdout.str()