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

Method test_raises_match

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