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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ Niclas Olofsson
Nicolas Delaby
Nicolas Simonds
Nico Vidal
Nikesh Chavhan
Nikolay Kondratyev
Nipunn Koorapati
Oleg Pidsadnyi
Expand Down
1 change: 1 addition & 0 deletions changelog/14026.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added test coverage for compiled regex patterns in :func:`pytest.raises` match parameter.
23 changes: 23 additions & 0 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,26 @@ def test_issue_11872(self) -> None:
code=404, msg="Not Found", fp=io.BytesIO(), hdrs=Message(), url=""
)
exc_info.value.close() # avoid a resource warning

def test_raises_match_compiled_regex(self) -> None:
"""Test that compiled regex patterns work with pytest.raises."""
# Test with a compiled pattern that matches
pattern = re.compile(r"with base \d+")
with pytest.raises(ValueError, match=pattern):
int("asdf")

# Test with a compiled pattern that doesn't match
pattern_nomatch = re.compile(r"with base 16")
expr = (
"Regex pattern did not match.\n"
f" Expected regex: {pattern_nomatch.pattern!r}\n"
f" Actual message: \"invalid literal for int() with base 10: 'asdf'\""
)
with pytest.raises(AssertionError, match="^" + re.escape(expr) + "$"):
with pytest.raises(ValueError, match=pattern_nomatch):
int("asdf", base=10)

# Test compiled pattern with flags
pattern_with_flags = re.compile(r"INVALID LITERAL", re.IGNORECASE)
with pytest.raises(ValueError, match=pattern_with_flags):
int("asdf")