Skip to content

Commit bec07d8

Browse files
committed
Update tests for re-emitted warnings
1 parent 05367e7 commit bec07d8

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed

testing/test_recwarn.py

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,21 @@ def test_deprecated_call_specificity(self) -> None:
203203
def f():
204204
warnings.warn(warning("hi"))
205205

206-
with pytest.raises(pytest.fail.Exception):
207-
pytest.deprecated_call(f)
208-
with pytest.raises(pytest.fail.Exception):
209-
with pytest.deprecated_call():
210-
f()
206+
with pytest.warns(warning):
207+
with pytest.raises(pytest.fail.Exception):
208+
pytest.deprecated_call(f)
209+
with pytest.raises(pytest.fail.Exception):
210+
with pytest.deprecated_call():
211+
f()
211212

212213
def test_deprecated_call_supports_match(self) -> None:
213214
with pytest.deprecated_call(match=r"must be \d+$"):
214215
warnings.warn("value must be 42", DeprecationWarning)
215216

216-
with pytest.raises(pytest.fail.Exception):
217-
with pytest.deprecated_call(match=r"must be \d+$"):
218-
warnings.warn("this is not here", DeprecationWarning)
217+
with pytest.deprecated_call():
218+
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
219+
with pytest.deprecated_call(match=r"must be \d+$"):
220+
warnings.warn("this is not here", DeprecationWarning)
219221

220222

221223
class TestWarns:
@@ -227,8 +229,9 @@ def test_check_callable(self) -> None:
227229
def test_several_messages(self) -> None:
228230
# different messages, b/c Python suppresses multiple identical warnings
229231
pytest.warns(RuntimeWarning, lambda: warnings.warn("w1", RuntimeWarning))
230-
with pytest.raises(pytest.fail.Exception):
231-
pytest.warns(UserWarning, lambda: warnings.warn("w2", RuntimeWarning))
232+
with pytest.warns(RuntimeWarning):
233+
with pytest.raises(pytest.fail.Exception):
234+
pytest.warns(UserWarning, lambda: warnings.warn("w2", RuntimeWarning))
232235
pytest.warns(RuntimeWarning, lambda: warnings.warn("w3", RuntimeWarning))
233236

234237
def test_function(self) -> None:
@@ -243,13 +246,14 @@ def test_warning_tuple(self) -> None:
243246
pytest.warns(
244247
(RuntimeWarning, SyntaxWarning), lambda: warnings.warn("w2", SyntaxWarning)
245248
)
246-
pytest.raises(
247-
pytest.fail.Exception,
248-
lambda: pytest.warns(
249-
(RuntimeWarning, SyntaxWarning),
250-
lambda: warnings.warn("w3", UserWarning),
251-
),
252-
)
249+
with pytest.warns():
250+
pytest.raises(
251+
pytest.fail.Exception,
252+
lambda: pytest.warns(
253+
(RuntimeWarning, SyntaxWarning),
254+
lambda: warnings.warn("w3", UserWarning),
255+
),
256+
)
253257

254258
def test_as_contextmanager(self) -> None:
255259
with pytest.warns(RuntimeWarning):
@@ -258,17 +262,19 @@ def test_as_contextmanager(self) -> None:
258262
with pytest.warns(UserWarning):
259263
warnings.warn("user", UserWarning)
260264

261-
with pytest.raises(pytest.fail.Exception) as excinfo:
262-
with pytest.warns(RuntimeWarning):
263-
warnings.warn("user", UserWarning)
265+
with pytest.warns():
266+
with pytest.raises(pytest.fail.Exception) as excinfo:
267+
with pytest.warns(RuntimeWarning):
268+
warnings.warn("user", UserWarning)
264269
excinfo.match(
265270
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted.\n"
266-
r"The list of emitted warnings is: \[UserWarning\('user',?\)\]."
271+
r" Emitted warnings: \[UserWarning\('user',?\)\]."
267272
)
268273

269-
with pytest.raises(pytest.fail.Exception) as excinfo:
270-
with pytest.warns(UserWarning):
271-
warnings.warn("runtime", RuntimeWarning)
274+
with pytest.warns():
275+
with pytest.raises(pytest.fail.Exception) as excinfo:
276+
with pytest.warns(UserWarning):
277+
warnings.warn("runtime", RuntimeWarning)
272278
excinfo.match(
273279
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n"
274280
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)]."
@@ -283,10 +289,11 @@ def test_as_contextmanager(self) -> None:
283289
)
284290

285291
warning_classes = (UserWarning, FutureWarning)
286-
with pytest.raises(pytest.fail.Exception) as excinfo:
287-
with pytest.warns(warning_classes) as warninfo:
288-
warnings.warn("runtime", RuntimeWarning)
289-
warnings.warn("import", ImportWarning)
292+
with pytest.warns():
293+
with pytest.raises(pytest.fail.Exception) as excinfo:
294+
with pytest.warns(warning_classes) as warninfo:
295+
warnings.warn("runtime", RuntimeWarning)
296+
warnings.warn("import", ImportWarning)
290297

291298
messages = [each.message for each in warninfo]
292299
expected_str = (
@@ -367,27 +374,31 @@ def test_match_regex(self) -> None:
367374
with pytest.warns(UserWarning, match=r"must be \d+$"):
368375
warnings.warn("value must be 42", UserWarning)
369376

370-
with pytest.raises(pytest.fail.Exception):
371-
with pytest.warns(UserWarning, match=r"must be \d+$"):
372-
warnings.warn("this is not here", UserWarning)
377+
with pytest.warns():
378+
with pytest.raises(pytest.fail.Exception):
379+
with pytest.warns(UserWarning, match=r"must be \d+$"):
380+
warnings.warn("this is not here", UserWarning)
373381

374-
with pytest.raises(pytest.fail.Exception):
375-
with pytest.warns(FutureWarning, match=r"must be \d+$"):
376-
warnings.warn("value must be 42", UserWarning)
382+
with pytest.warns():
383+
with pytest.raises(pytest.fail.Exception):
384+
with pytest.warns(FutureWarning, match=r"must be \d+$"):
385+
warnings.warn("value must be 42", UserWarning)
377386

378387
def test_one_from_multiple_warns(self) -> None:
379-
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
380-
with pytest.warns(UserWarning, match=r"aaa"):
388+
with pytest.warns():
389+
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
381390
with pytest.warns(UserWarning, match=r"aaa"):
382-
warnings.warn("cccccccccc", UserWarning)
383-
warnings.warn("bbbbbbbbbb", UserWarning)
384-
warnings.warn("aaaaaaaaaa", UserWarning)
391+
with pytest.warns(UserWarning, match=r"aaa"):
392+
warnings.warn("cccccccccc", UserWarning)
393+
warnings.warn("bbbbbbbbbb", UserWarning)
394+
warnings.warn("aaaaaaaaaa", UserWarning)
385395

386396
def test_none_of_multiple_warns(self) -> None:
387-
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
388-
with pytest.warns(UserWarning, match=r"aaa"):
389-
warnings.warn("bbbbbbbbbb", UserWarning)
390-
warnings.warn("cccccccccc", UserWarning)
397+
with pytest.warns():
398+
with pytest.raises(pytest.fail.Exception, match="DID NOT WARN"):
399+
with pytest.warns(UserWarning, match=r"aaa"):
400+
warnings.warn("bbbbbbbbbb", UserWarning)
401+
warnings.warn("cccccccccc", UserWarning)
391402

392403
@pytest.mark.filterwarnings("ignore")
393404
def test_can_capture_previously_warned(self) -> None:

0 commit comments

Comments
 (0)