Skip to content

Commit 38baa8e

Browse files
enhance rec-warn error
1 parent 8300b26 commit 38baa8e

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/_pytest/recwarn.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,19 @@ def __exit__(
268268

269269
__tracebackhide__ = True
270270

271+
def found_str():
272+
found = "".join(f" {each.message!r},\n" for each in self)
273+
return f"\n{found}" if found else ""
274+
271275
# only check if we're not currently handling an exception
272276
if exc_type is None and exc_val is None and exc_tb is None:
273277
if self.expected_warning is not None:
274278
if not any(issubclass(r.category, self.expected_warning) for r in self):
275279
__tracebackhide__ = True
276280
fail(
277-
"DID NOT WARN. No warnings of type {} was emitted. "
278-
"The list of emitted warnings is: {}.".format(
279-
self.expected_warning, [each.message for each in self]
281+
"DID NOT WARN. No warnings of type {expected} was emitted. "
282+
"The list of emitted warnings is: [{found}].".format(
283+
expected=self.expected_warning, found=found_str()
280284
)
281285
)
282286
elif self.match_expr is not None:
@@ -286,11 +290,13 @@ def __exit__(
286290
break
287291
else:
288292
fail(
289-
"DID NOT WARN. No warnings of type {} matching"
290-
" ('{}') was emitted. The list of emitted warnings"
291-
" is: {}.".format(
292-
self.expected_warning,
293-
self.match_expr,
294-
[each.message for each in self],
293+
"DID NOT WARN. No warnings of type {expected} matching the"
294+
" regex was emitted.\n"
295+
"The regex is: {match!r}\n"
296+
"The list of emitted warnings"
297+
" is: [{found}].".format(
298+
expected=self.expected_warning,
299+
match=self.match_expr,
300+
found=found_str(),
295301
)
296302
)

testing/test_recwarn.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import re
21
import warnings
32
from typing import Optional
43

@@ -251,23 +250,25 @@ def test_as_contextmanager(self) -> None:
251250
with pytest.raises(pytest.fail.Exception) as excinfo:
252251
with pytest.warns(RuntimeWarning):
253252
warnings.warn("user", UserWarning)
254-
excinfo.match(
253+
assert excinfo.match(
255254
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) was emitted. "
256-
r"The list of emitted warnings is: \[UserWarning\('user',?\)\]."
255+
r"The list of emitted warnings is: \[\n"
256+
r" UserWarning\('user'\),\n"
257+
r"\]."
257258
)
258259

259260
with pytest.raises(pytest.fail.Exception) as excinfo:
260261
with pytest.warns(UserWarning):
261262
warnings.warn("runtime", RuntimeWarning)
262-
excinfo.match(
263+
assert excinfo.match(
263264
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. "
264-
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)\]."
265+
r"The list of emitted warnings is: \[\n RuntimeWarning\('runtime',?\),\n\]."
265266
)
266267

267268
with pytest.raises(pytest.fail.Exception) as excinfo:
268269
with pytest.warns(UserWarning):
269270
pass
270-
excinfo.match(
271+
assert excinfo.match(
271272
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. "
272273
r"The list of emitted warnings is: \[\]."
273274
)
@@ -280,14 +281,11 @@ def test_as_contextmanager(self) -> None:
280281

281282
message_template = (
282283
"DID NOT WARN. No warnings of type {0} was emitted. "
283-
"The list of emitted warnings is: {1}."
284+
"The list of emitted warnings is: [\n{1}]."
284285
)
285-
excinfo.match(
286-
re.escape(
287-
message_template.format(
288-
warning_classes, [each.message for each in warninfo]
289-
)
290-
)
286+
287+
assert str(excinfo.value) == message_template.format(
288+
warning_classes, "".join(f" {each.message!r},\n" for each in warninfo)
291289
)
292290

293291
def test_record(self) -> None:

0 commit comments

Comments
 (0)