Skip to content

Commit 6df19b4

Browse files
committed
Update tests for re-emitted warnings
1 parent 7488dfc commit 6df19b4

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/_pytest/recwarn.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ def warns( # noqa: F811
135135
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
136136
... warnings.warn("value must be 42", UserWarning)
137137
138-
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
139-
... warnings.warn("this is not here", UserWarning)
138+
>>> with pytest.warns(UserWarning): # catch re-emitted warning
139+
... with pytest.warns(UserWarning, match=r'must be \d+$'):
140+
... warnings.warn("this is not here", UserWarning)
140141
Traceback (most recent call last):
141142
...
142143
Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted...
@@ -281,7 +282,8 @@ def __init__(
281282
self.expected_warning = expected_warning_tup
282283
self.match_expr = match_expr
283284

284-
def matches(self, warning) -> bool:
285+
def matches(self, warning: warnings.WarningMessage) -> bool:
286+
assert self.expected_warning is not None
285287
return issubclass(warning.category, self.expected_warning) and (
286288
self.match_expr is None or re.search(self.match_expr, str(warning.message))
287289
)
@@ -326,7 +328,7 @@ def found_str():
326328
if not self.matches(w):
327329
warnings.warn_explicit(
328330
str(w.message),
329-
w.message.__class__,
331+
w.message.__class__, # type: ignore
330332
w.filename,
331333
w.lineno,
332334
module=w.__module__,

testing/test_recwarn.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,15 @@ def test_as_contextmanager(self) -> None:
277277
warnings.warn("runtime", RuntimeWarning)
278278
excinfo.match(
279279
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n"
280-
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)]."
280+
r" Emitted warnings: \[RuntimeWarning\('runtime',?\)]."
281281
)
282282

283283
with pytest.raises(pytest.fail.Exception) as excinfo:
284284
with pytest.warns(UserWarning):
285285
pass
286286
excinfo.match(
287287
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n"
288-
r"The list of emitted warnings is: \[\]."
288+
r" Emitted warnings: \[\]."
289289
)
290290

291291
warning_classes = (UserWarning, FutureWarning)
@@ -298,7 +298,7 @@ def test_as_contextmanager(self) -> None:
298298
messages = [each.message for each in warninfo]
299299
expected_str = (
300300
f"DID NOT WARN. No warnings of type {warning_classes} were emitted.\n"
301-
f"The list of emitted warnings is: {messages}."
301+
f" Emitted warnings: {messages}."
302302
)
303303

304304
assert str(excinfo.value) == expected_str

0 commit comments

Comments
 (0)