classification
Title: assertWarns and assertWarnsRegexp
Type: enhancement Stage: committed/rejected
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, ezio.melotti, flox, giampaolo.rodola, michael.foord, pitrou
Priority: normal Keywords: patch

Created on 2010-09-03 10:50 by pitrou, last changed 2010-11-01 15:00 by ezio.melotti. 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
2010-11-01 15:00:13ezio.melottisetnosy: + ezio.melotti
2010-09-06 19:26:14pitrousetstatus: open -> closed
resolution: fixed
messages: + msg115730

stage: patch review -> committed/rejected
2010-09-04 20:33:53pitrousetfiles: + assertwarns2.patch

messages: + msg115614
2010-09-03 23:06:39floxlinkissue8273 dependencies
2010-09-03 17:12:58pitrousetmessages: + msg115471
2010-09-03 17:08:35michael.foordsetmessages: + msg115470
2010-09-03 17:05:04pitrousetmessages: + msg115469
2010-09-03 16:45:57floxsetmessages: + msg115465
stage: needs patch -> patch review
2010-09-03 15:19:42michael.foordsetmessages: + msg115460
2010-09-03 14:13:13pitrousetfiles: - assertwarns.patch
2010-09-03 14:13:08pitrousetfiles: + assertwarns.patch

messages: + msg115455
2010-09-03 13:59:45pitrousetfiles: + assertwarns.patch
keywords: + patch
messages: + msg115453
2010-09-03 12:55:19giampaolo.rodolasetnosy: + giampaolo.rodola
messages: + msg115451
2010-09-03 12:51:05floxsetnosy: + flox
messages: + msg115450
2010-09-03 12:44:54pitrousetmessages: + msg115449
2010-09-03 11:04:15michael.foordsetmessages: + msg115436
2010-09-03 10:50:15pitroucreate