Update tests for re-emitted warnings

This commit is contained in:
Zac Hatfield-Dodds 2023-06-30 15:29:03 -07:00
parent a1b37022af
commit 7022fb455d
2 changed files with 61 additions and 49 deletions

View File

@ -135,8 +135,9 @@ def warns( # noqa: F811
>>> with pytest.warns(UserWarning, match=r'must be \d+$'): >>> with pytest.warns(UserWarning, match=r'must be \d+$'):
... warnings.warn("value must be 42", UserWarning) ... warnings.warn("value must be 42", UserWarning)
>>> with pytest.warns(UserWarning, match=r'must be \d+$'): >>> with pytest.warns(UserWarning): # catch re-emitted warning
... warnings.warn("this is not here", UserWarning) ... with pytest.warns(UserWarning, match=r'must be \d+$'):
... warnings.warn("this is not here", UserWarning)
Traceback (most recent call last): Traceback (most recent call last):
... ...
Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted... Failed: DID NOT WARN. No warnings of type ...UserWarning... were emitted...
@ -327,7 +328,7 @@ class WarningsChecker(WarningsRecorder):
if not self.matches(w): if not self.matches(w):
warnings.warn_explicit( warnings.warn_explicit(
str(w.message), str(w.message),
w.message.__class__, w.message.__class__, # type: ignore
w.filename, w.filename,
w.lineno, w.lineno,
module=w.__module__, module=w.__module__,

View File

@ -203,19 +203,21 @@ class TestDeprecatedCall:
def f(): def f():
warnings.warn(warning("hi")) warnings.warn(warning("hi"))
with pytest.raises(pytest.fail.Exception): with pytest.warns(warning):
pytest.deprecated_call(f) with pytest.raises(pytest.fail.Exception):
with pytest.raises(pytest.fail.Exception): pytest.deprecated_call(f)
with pytest.deprecated_call(): with pytest.raises(pytest.fail.Exception):
f() with pytest.deprecated_call():
f()
def test_deprecated_call_supports_match(self) -> None: def test_deprecated_call_supports_match(self) -> None:
with pytest.deprecated_call(match=r"must be \d+$"): with pytest.deprecated_call(match=r"must be \d+$"):
warnings.warn("value must be 42", DeprecationWarning) warnings.warn("value must be 42", DeprecationWarning)
with pytest.raises(pytest.fail.Exception): with pytest.deprecated_call():
with pytest.deprecated_call(match=r"must be \d+$"): with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
warnings.warn("this is not here", DeprecationWarning) with pytest.deprecated_call(match=r"must be \d+$"):
warnings.warn("this is not here", DeprecationWarning)
class TestWarns: class TestWarns:
@ -227,8 +229,9 @@ class TestWarns:
def test_several_messages(self) -> None: def test_several_messages(self) -> None:
# different messages, b/c Python suppresses multiple identical warnings # different messages, b/c Python suppresses multiple identical warnings
pytest.warns(RuntimeWarning, lambda: warnings.warn("w1", RuntimeWarning)) pytest.warns(RuntimeWarning, lambda: warnings.warn("w1", RuntimeWarning))
with pytest.raises(pytest.fail.Exception): with pytest.warns(RuntimeWarning):
pytest.warns(UserWarning, lambda: warnings.warn("w2", RuntimeWarning)) with pytest.raises(pytest.fail.Exception):
pytest.warns(UserWarning, lambda: warnings.warn("w2", RuntimeWarning))
pytest.warns(RuntimeWarning, lambda: warnings.warn("w3", RuntimeWarning)) pytest.warns(RuntimeWarning, lambda: warnings.warn("w3", RuntimeWarning))
def test_function(self) -> None: def test_function(self) -> None:
@ -243,13 +246,14 @@ class TestWarns:
pytest.warns( pytest.warns(
(RuntimeWarning, SyntaxWarning), lambda: warnings.warn("w2", SyntaxWarning) (RuntimeWarning, SyntaxWarning), lambda: warnings.warn("w2", SyntaxWarning)
) )
pytest.raises( with pytest.warns():
pytest.fail.Exception, pytest.raises(
lambda: pytest.warns( pytest.fail.Exception,
(RuntimeWarning, SyntaxWarning), lambda: pytest.warns(
lambda: warnings.warn("w3", UserWarning), (RuntimeWarning, SyntaxWarning),
), lambda: warnings.warn("w3", UserWarning),
) ),
)
def test_as_contextmanager(self) -> None: def test_as_contextmanager(self) -> None:
with pytest.warns(RuntimeWarning): with pytest.warns(RuntimeWarning):
@ -258,20 +262,22 @@ class TestWarns:
with pytest.warns(UserWarning): with pytest.warns(UserWarning):
warnings.warn("user", UserWarning) warnings.warn("user", UserWarning)
with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns():
with pytest.warns(RuntimeWarning): with pytest.raises(pytest.fail.Exception) as excinfo:
warnings.warn("user", UserWarning) with pytest.warns(RuntimeWarning):
warnings.warn("user", UserWarning)
excinfo.match( excinfo.match(
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted.\n" r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted.\n"
r"The list of emitted warnings is: \[UserWarning\('user',?\)\]." r" Emitted warnings: \[UserWarning\('user',?\)\]."
) )
with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns():
with pytest.warns(UserWarning): with pytest.raises(pytest.fail.Exception) as excinfo:
warnings.warn("runtime", RuntimeWarning) with pytest.warns(UserWarning):
warnings.warn("runtime", RuntimeWarning)
excinfo.match( excinfo.match(
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n" 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.raises(pytest.fail.Exception) as excinfo:
@ -279,19 +285,20 @@ class TestWarns:
pass pass
excinfo.match( excinfo.match(
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n" 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) warning_classes = (UserWarning, FutureWarning)
with pytest.raises(pytest.fail.Exception) as excinfo: with pytest.warns():
with pytest.warns(warning_classes) as warninfo: with pytest.raises(pytest.fail.Exception) as excinfo:
warnings.warn("runtime", RuntimeWarning) with pytest.warns(warning_classes) as warninfo:
warnings.warn("import", ImportWarning) warnings.warn("runtime", RuntimeWarning)
warnings.warn("import", ImportWarning)
messages = [each.message for each in warninfo] messages = [each.message for each in warninfo]
expected_str = ( expected_str = (
f"DID NOT WARN. No warnings of type {warning_classes} were emitted.\n" 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 assert str(excinfo.value) == expected_str
@ -367,27 +374,31 @@ class TestWarns:
with pytest.warns(UserWarning, match=r"must be \d+$"): with pytest.warns(UserWarning, match=r"must be \d+$"):
warnings.warn("value must be 42", UserWarning) warnings.warn("value must be 42", UserWarning)
with pytest.raises(pytest.fail.Exception): with pytest.warns():
with pytest.warns(UserWarning, match=r"must be \d+$"): with pytest.raises(pytest.fail.Exception):
warnings.warn("this is not here", UserWarning) with pytest.warns(UserWarning, match=r"must be \d+$"):
warnings.warn("this is not here", UserWarning)
with pytest.raises(pytest.fail.Exception): with pytest.warns():
with pytest.warns(FutureWarning, match=r"must be \d+$"): with pytest.raises(pytest.fail.Exception):
warnings.warn("value must be 42", UserWarning) with pytest.warns(FutureWarning, match=r"must be \d+$"):
warnings.warn("value must be 42", UserWarning)
def test_one_from_multiple_warns(self) -> None: def test_one_from_multiple_warns(self) -> None:
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"): with pytest.warns():
with pytest.warns(UserWarning, match=r"aaa"): with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
with pytest.warns(UserWarning, match=r"aaa"): with pytest.warns(UserWarning, match=r"aaa"):
warnings.warn("cccccccccc", UserWarning) with pytest.warns(UserWarning, match=r"aaa"):
warnings.warn("bbbbbbbbbb", UserWarning) warnings.warn("cccccccccc", UserWarning)
warnings.warn("aaaaaaaaaa", UserWarning) warnings.warn("bbbbbbbbbb", UserWarning)
warnings.warn("aaaaaaaaaa", UserWarning)
def test_none_of_multiple_warns(self) -> None: def test_none_of_multiple_warns(self) -> None:
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"): with pytest.warns():
with pytest.warns(UserWarning, match=r"aaa"): with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
warnings.warn("bbbbbbbbbb", UserWarning) with pytest.warns(UserWarning, match=r"aaa"):
warnings.warn("cccccccccc", UserWarning) warnings.warn("bbbbbbbbbb", UserWarning)
warnings.warn("cccccccccc", UserWarning)
@pytest.mark.filterwarnings("ignore") @pytest.mark.filterwarnings("ignore")
def test_can_capture_previously_warned(self) -> None: def test_can_capture_previously_warned(self) -> None: