Issue 9754: assertWarns and assertWarnsRegexp (original) (raw)

Created on 2010-09-03 10:50 by pitrou, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
assertwarns.patch pitrou,2010-09-03 14:13
assertwarns2.patch pitrou,2010-09-04 20:33
Messages (14)
msg115434 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-03 10:50
Similar to assertRaises and assertRaisesRegexp, unittest should provide assertWarns and assertWarnsRegexp, to check that a given callable (or piece of code) triggers a particular warning. Currently, you have to do that manually using a mixture of warnings.catch_warnings and warnings.filterwarnings, which is pretty annoying.
msg115436 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-09-03 11:04
+1
msg115449 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-03 12:44
The __warningregistry__ stuff looks horrible.
msg115450 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-09-03 12:51
> unittest should provide assertWarns and assertWarnsRegexp +1 (the internal helpers in test.support could be refactored) > The __warningregistry__ stuff looks horrible. +1 (and it does not behave exactly the same in all Python versions)
msg115451 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2010-09-03 12:55
+1 from me as well.
msg115453 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-03 13:59
Here is a patch. The approach is different from support.check_warnings(), and tries to mimic assertRaises* instead.
msg115455 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-03 14:13
There was some dead code in the patch.
msg115460 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-09-03 15:19
(Note that in general I am against extending the TestCase API with more asserts given how wide it is and how much it has expanded in recent versions. I've written warning checking code enough times for third party projects that I think this is worth it though.)
msg115465 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-09-03 16:45
Patch looks good. However tests do not pass with -Werror (while test_warnings and others pass). Is there a way to catch multiple warnings on a single logical line? (With assertRaises we don't have such use case)
msg115469 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-03 17:05
> Patch looks good. However tests do not pass with -Werror (while test_warnings and others pass). > > > Is there a way to catch multiple warnings on a single logical line? I thought we could beef up the API with additional arguments. That would be at the expense of removing the "callable" argument (and mandating the use as a context manager), though, otherwise the signature gets too complicated.
msg115470 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-09-03 17:08
Why not accepting a tuple of warnings? That doesn't make sense for assertWarnsRegexp of course.
msg115471 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-03 17:12
> Why not accepting a tuple of warnings? It already does (and there's a test!). > That doesn't make sense for assertWarnsRegexp of course. True.
msg115614 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-04 20:33
Updated patch so that the tests pass with -Werror. Do you think this should be committed before the next alpha?
msg115730 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-06 19:26
Committed in r84563!
History
Date User Action Args
2022-04-11 14:57:06 admin set github: 53963
2010-11-01 15:00:13 ezio.melotti set nosy: + ezio.melotti
2010-09-06 19:26:14 pitrou set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2010-09-04 20:33:53 pitrou set files: + assertwarns2.patchmessages: +
2010-09-03 23:06:39 flox link issue8273 dependencies
2010-09-03 17:12:58 pitrou set messages: +
2010-09-03 17:08:35 michael.foord set messages: +
2010-09-03 17:05:04 pitrou set messages: +
2010-09-03 16:45:57 flox set messages: + stage: needs patch -> patch review
2010-09-03 15:19:42 michael.foord set messages: +
2010-09-03 14:13:13 pitrou set files: - assertwarns.patch
2010-09-03 14:13:08 pitrou set files: + assertwarns.patchmessages: +
2010-09-03 13:59:45 pitrou set files: + assertwarns.patchkeywords: + patchmessages: +
2010-09-03 12:55:19 giampaolo.rodola set nosy: + giampaolo.rodolamessages: +
2010-09-03 12:51:05 flox set nosy: + floxmessages: +
2010-09-03 12:44:54 pitrou set messages: +
2010-09-03 11:04:15 michael.foord set messages: +
2010-09-03 10:50:15 pitrou create