MCPcopy
hub / github.com/pytest-dev/pytest / test_raises_match_compiled_regex

Method test_raises_match_compiled_regex

testing/python/raises.py:443–464  ·  view source on GitHub ↗

Test that compiled regex patterns work with pytest.raises.

(self)

Source from the content-addressed store, hash-verified

441 exc_info.value.close() # avoid a resource warning
442
443 def test_raises_match_compiled_regex(self) -> None:
444 """Test that compiled regex patterns work with pytest.raises."""
445 # Test with a compiled pattern that matches
446 pattern = re.compile(r"with base \d+")
447 with pytest.raises(ValueError, match=pattern):
448 int("asdf")
449
450 # Test with a compiled pattern that doesn't match
451 pattern_nomatch = re.compile(r"with base 16")
452 expr = (
453 "Regex pattern did not match.\n"
454 f" Expected regex: {pattern_nomatch.pattern!r}\n"
455 f" Actual message: \"invalid literal for int() with base 10: 'asdf'\""
456 )
457 with pytest.raises(AssertionError, match="^" + re.escape(expr) + "$"):
458 with pytest.raises(ValueError, match=pattern_nomatch):
459 int("asdf", base=10)
460
461 # Test compiled pattern with flags
462 pattern_with_flags = re.compile(r"INVALID LITERAL", re.IGNORECASE)
463 with pytest.raises(ValueError, match=pattern_with_flags):
464 int("asdf")
465
466 def test_pipe_is_treated_as_regex_metacharacter(self) -> None:
467 """| (pipe) must be recognized as a regex metacharacter."""

Callers

nothing calls this directly

Calls 2

intClass · 0.85
compileMethod · 0.80

Tested by

no test coverage detected