Skip to content

Commit 87e9209

Browse files
enhance rec-warn error
* uses multiple lines for found warnings
1 parent 0fbfd1c commit 87e9209

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/_pytest/recwarn.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def warns(
136136
... warnings.warn("this is not here", UserWarning)
137137
Traceback (most recent call last):
138138
...
139-
Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted...
139+
Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted...
140140
141141
"""
142142
__tracebackhide__ = True
@@ -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} were 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 & 14 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

@@ -263,16 +262,18 @@ def test_as_contextmanager(self) -> None:
263262
with pytest.warns(RuntimeWarning):
264263
warnings.warn("user", UserWarning)
265264
excinfo.match(
266-
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) was emitted. "
267-
r"The list of emitted warnings is: \[UserWarning\('user',?\)\]."
265+
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted. "
266+
r"The list of emitted warnings is: \[\n"
267+
r" UserWarning\('user'\),\n"
268+
r"\]."
268269
)
269270

270271
with pytest.raises(pytest.fail.Exception) as excinfo:
271272
with pytest.warns(UserWarning):
272273
warnings.warn("runtime", RuntimeWarning)
273274
excinfo.match(
274275
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. "
275-
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)\]."
276+
r"The list of emitted warnings is: \[\n RuntimeWarning\('runtime',?\),\n\]."
276277
)
277278

278279
with pytest.raises(pytest.fail.Exception) as excinfo:
@@ -289,18 +290,14 @@ def test_as_contextmanager(self) -> None:
289290
warnings.warn("runtime", RuntimeWarning)
290291
warnings.warn("import", ImportWarning)
291292

292-
message_template = (
293-
"DID NOT WARN. No warnings of type {0} was emitted. "
294-
"The list of emitted warnings is: {1}."
295-
)
296-
excinfo.match(
297-
re.escape(
298-
message_template.format(
299-
warning_classes, [each.message for each in warninfo]
300-
)
301-
)
293+
messages = "".join(f" {each.message!r},\n" for each in warninfo)
294+
expected_str = (
295+
f"DID NOT WARN. No warnings of type {warning_classes} was emitted. "
296+
f"The list of emitted warnings is: [\n{messages}]."
302297
)
303298

299+
assert str(excinfo.value) == expected_str
300+
304301
def test_record(self) -> None:
305302
with pytest.warns(UserWarning) as record:
306303
warnings.warn("user", UserWarning)

0 commit comments

Comments
 (0)