test.support.check_warnings
The docs say this [1]: ========================================================== test.support.check_warnings(*filters, quiet=True) A convenience wrapper for warnings.catch_warnings() that makes it easier to test that a warning was correctly raised. It is approximately equivalent to calling warnings.catch_warnings(record=True) with warnings.simplefilter() set to always and with the option to automatically validate the results that are recorded. check_warnings accepts 2-tuples of the form ("message regexp", WarningCategory) as positional arguments. If one or more filters are provided, or if the optional keyword argument quiet is False, it checks to make sure the warnings are as expected: each specified filter must match at least one of the warnings raised by the enclosed code or the test fails, and if any warnings are raised that do not match any of the specified filters the test fails. To disable the first of these checks, set quiet to True. ========================================================== What I want is to make sure that DeprecationWarnings are being raised: ========================================================== with support.check_warnings( ("automatic int conversions have been deprecated", DeprecationWarning), quiet=False, ): exec("'%x' % pi") exec("'%x' % 3.14") exec("'%X' % 2.11") exec("'%o' % 1.79") exec("'%c' % pi") ========================================================== But if I throw in something that doesn't raise a deprecation warning, the test still passes: ========================================================== exec("'%d' % 3") ========================================================== Am I doing something wrong? -- ~Ethan~
On Sat, Jan 11, 2014 at 3:45 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
The docs say this [1]: ========================================================== test.support.check_warnings(*filters, quiet=True)
A convenience wrapper for warnings.catch_warnings() that makes it easier to test that a warning was correctly raised. It is approximately equivalent to calling warnings.catch_warnings(record=True) with warnings.simplefilter() set to always and with the option to automatically validate the results that are recorded.
check_warnings accepts 2-tuples of the form ("message regexp", WarningCategory) as positional arguments. If one or more filters are provided, or if the optional keyword argument quiet is False, it checks to make sure the warnings are as expected: each specified filter must match at least one of the warnings raised by the enclosed code or the test fails, and if any warnings are raised that do not match any of the specified filters the test fails. To disable the first of these checks, set quiet to True. ==========================================================
What I want is to make sure that DeprecationWarnings are being raised: ========================================================== with support.check_warnings( ("automatic int conversions have been deprecated", DeprecationWarning), quiet=False, ): exec("'%x' % pi") exec("'%x' % 3.14") exec("'%X' % 2.11") exec("'%o' % 1.79") exec("'%c' % pi") ==========================================================
But if I throw in something that doesn't raise a deprecation warning, the test still passes: ========================================================== exec("'%d' % 3") ==========================================================
Am I doing something wrong?
You're assuming the context manager is doing something magical to verify that all calls in the block raise the expected exception. What you want to do is execute it in a loop:: for test in (...): with support.check_warnings(("automatic int conversions have been deprecated", DeprecationWarning), quiet=False): exec(test)
On 01/11/2014 05:37 PM, Brett Cannon wrote:
You're assuming the context manager is doing something magical to verify that all calls in the block raise the expected exception. What you want to do is execute it in a loop::
for test in (...): with support.check_warnings(("automatic int conversions have been deprecated", DeprecationWarning), quiet=False): exec(test)
Well, this is test.support! I expect magic! ;) Thanks for setting me straight, got it working. -- ~Ethan~
On Sat, 11 Jan 2014 23:10:43 -0800 Ethan Furman <ethan@stoneleaf.us> wrote:
On 01/11/2014 05:37 PM, Brett Cannon wrote:
You're assuming the context manager is doing something magical to verify that all calls in the block raise the expected exception. What you want to do is execute it in a loop::
for test in (...): with support.check_warnings(("automatic int conversions have been deprecated", DeprecationWarning), quiet=False): exec(test)
Well, this is test.support! I expect magic! ;)
Thanks for setting me straight, got it working.
Or you could, you know, use the new assertWarns(): http://docs.python.org/dev/library/unittest.html#unittest.TestCase.assertWar... Regards Antoine.
On 01/12/2014 04:24 AM, Antoine Pitrou wrote:
On Sat, 11 Jan 2014 23:10:43 -0800 Ethan Furman <ethan@stoneleaf.us> wrote:
On 01/11/2014 05:37 PM, Brett Cannon wrote:
You're assuming the context manager is doing something magical to verify that all calls in the block raise the expected exception. What you want to do is execute it in a loop::
for test in (...): with support.check_warnings(("automatic int conversions have been deprecated", DeprecationWarning), quiet=False): exec(test)
Well, this is test.support! I expect magic! ;)
Thanks for setting me straight, got it working.
Or you could, you know, use the new assertWarns(): http://docs.python.org/dev/library/unittest.html#unittest.TestCase.assertWar...
That's also cool. If I have to touch that code again I'll switch to it. -- ~Ethan~
participants (3)
-
Antoine Pitrou
-
Brett Cannon
-
Ethan Furman