Skip to content

Commit

Permalink
Update tests for re-emitted warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jun 27, 2023
1 parent 7488dfc commit 6df19b4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ def warns( # noqa: F811
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
... warnings.warn("value must be 42", UserWarning)
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
... warnings.warn("this is not here", UserWarning)
>>> with pytest.warns(UserWarning): # catch re-emitted warning
... with pytest.warns(UserWarning, match=r'must be \d+$'):
... warnings.warn("this is not here", UserWarning)
Traceback (most recent call last):
...
Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted...
Expand Down Expand Up @@ -281,7 +282,8 @@ def __init__(
self.expected_warning = expected_warning_tup
self.match_expr = match_expr

def matches(self, warning) -> bool:
def matches(self, warning: warnings.WarningMessage) -> bool:
assert self.expected_warning is not None
return issubclass(warning.category, self.expected_warning) and (
self.match_expr is None or re.search(self.match_expr, str(warning.message))
)
Expand Down Expand Up @@ -326,7 +328,7 @@ def found_str():
if not self.matches(w):
warnings.warn_explicit(
str(w.message),
w.message.__class__,
w.message.__class__, # type: ignore
w.filename,
w.lineno,
module=w.__module__,
Expand Down
6 changes: 3 additions & 3 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ def test_as_contextmanager(self) -> None:
warnings.warn("runtime", RuntimeWarning)
excinfo.match(
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n"
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)]."
r" Emitted warnings: \[RuntimeWarning\('runtime',?\)]."
)

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

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

assert str(excinfo.value) == expected_str
Expand Down

0 comments on commit 6df19b4

Please sign in to comment.