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

Method test_raises_match

testing/python/raises.py:275–316  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

273 assert refcount == len(gc.get_referrers(t))
274
275 def test_raises_match(self) -> None:
276 msg = r"with base \d+"
277 with pytest.raises(ValueError, match=msg):
278 int("asdf")
279
280 msg = "with base 10"
281 with pytest.raises(ValueError, match=msg):
282 int("asdf")
283
284 msg = "with base 16"
285 expr = (
286 "Regex pattern did not match.\n"
287 f" Expected regex: {msg!r}\n"
288 f" Actual message: \"invalid literal for int() with base 10: 'asdf'\""
289 )
290 with pytest.raises(AssertionError, match="^" + re.escape(expr) + "$"):
291 with pytest.raises(ValueError, match=msg):
292 int("asdf", base=10)
293
294 # "match" without context manager.
295 pytest.raises(ValueError, int, "asdf").match("invalid literal")
296 with pytest.raises(AssertionError) as excinfo:
297 pytest.raises(ValueError, int, "asdf").match(msg)
298 assert str(excinfo.value) == expr
299
300 pytest.raises(TypeError, int, match="invalid") # type: ignore[call-overload]
301
302 def tfunc(match):
303 raise ValueError(f"match={match}")
304
305 pytest.raises(ValueError, tfunc, match="asdf").match("match=asdf")
306 pytest.raises(ValueError, tfunc, match="").match("match=")
307
308 # empty string matches everything, which is probably not what the user wants
309 with pytest.warns(
310 PytestWarning,
311 match=wrap_escape(
312 "matching against an empty string will *always* pass. If you want to check for an empty message you "
313 "need to pass '^$'. If you don't want to match you should pass `None` or leave out the parameter."
314 ),
315 ):
316 pytest.raises(match="")
317
318 def test_match_failure_string_quoting(self):
319 with pytest.raises(AssertionError) as excinfo:

Callers

nothing calls this directly

Calls 3

intClass · 0.85
matchMethod · 0.80
wrap_escapeFunction · 0.70

Tested by

no test coverage detected