Closed pitrou closed 13 years ago
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.
+1
The __warningregistry__ stuff looks horrible.
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)
+1 from me as well.
Here is a patch. The approach is different from support.check_warnings(), and tries to mimic assertRaises* instead.
There was some dead code in the patch.
(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.)
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)
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.
Why not accepting a tuple of warnings? That doesn't make sense for assertWarnsRegexp of course.
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.
Updated patch so that the tests pass with -Werror. Do you think this should be committed before the next alpha?
Committed in r84563!
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = None closed_at =
created_at =
labels = ['type-feature', 'library']
title = 'assertWarns and assertWarnsRegexp'
updated_at =
user = 'https://github.com/pitrou'
```
bugs.python.org fields:
```python
activity =
actor = 'ezio.melotti'
assignee = 'none'
closed = True
closed_date =
closer = 'pitrou'
components = ['Library (Lib)']
creation =
creator = 'pitrou'
dependencies = []
files = ['18728', '18756']
hgrepos = []
issue_num = 9754
keywords = ['patch']
message_count = 14.0
messages = ['115434', '115436', '115449', '115450', '115451', '115453', '115455', '115460', '115465', '115469', '115470', '115471', '115614', '115730']
nosy_count = 6.0
nosy_names = ['brett.cannon', 'pitrou', 'giampaolo.rodola', 'ezio.melotti', 'michael.foord', 'flox']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue9754'
versions = ['Python 3.2']
```