Skip to content

Commit 24d9a2f

Browse files
enhance rec-warn error
* uses multiple lines for found warnings
1 parent 8300b26 commit 24d9a2f

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-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: 10 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

@@ -253,15 +252,17 @@ def test_as_contextmanager(self) -> None:
253252
warnings.warn("user", UserWarning)
254253
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)
262263
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:
@@ -278,18 +279,14 @@ def test_as_contextmanager(self) -> None:
278279
warnings.warn("runtime", RuntimeWarning)
279280
warnings.warn("import", ImportWarning)
280281

281-
message_template = (
282-
"DID NOT WARN. No warnings of type {0} was emitted. "
283-
"The list of emitted warnings is: {1}."
284-
)
285-
excinfo.match(
286-
re.escape(
287-
message_template.format(
288-
warning_classes, [each.message for each in warninfo]
289-
)
290-
)
282+
messages = "".join(f" {each.message!r},\n" for each in warninfo)
283+
expected_str = (
284+
f"DID NOT WARN. No warnings of type {warning_classes} was emitted. "
285+
f"The list of emitted warnings is: [\n{messages}]."
291286
)
292287

288+
assert str(excinfo.value) == expected_str
289+
293290
def test_record(self) -> None:
294291
with pytest.warns(UserWarning) as record:
295292
warnings.warn("user", UserWarning)

0 commit comments

Comments
 (0)