Kaydet (Commit) 73588542 authored tarafından Florent Xicluna's avatar Florent Xicluna

#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.

üst f3e9b2a9
This diff is collapsed.
...@@ -577,14 +577,19 @@ def check_warnings(*filters, **kwargs): ...@@ -577,14 +577,19 @@ def check_warnings(*filters, **kwargs):
Optional argument: Optional argument:
- if 'quiet' is True, it does not fail if a filter catches nothing - if 'quiet' is True, it does not fail if a filter catches nothing
(default False) (default True without argument,
default False if some filters are defined)
Without argument, it defaults to: Without argument, it defaults to:
check_warnings(("", Warning), quiet=False) check_warnings(("", Warning), quiet=True)
""" """
quiet = kwargs.get('quiet')
if not filters: if not filters:
filters = (("", Warning),) filters = (("", Warning),)
return _filterwarnings(filters, kwargs.get('quiet')) # Preserve backward compatibility
if quiet is None:
quiet = True
return _filterwarnings(filters, quiet)
@contextlib.contextmanager @contextlib.contextmanager
......
...@@ -633,19 +633,33 @@ class CatchWarningTests(BaseTest): ...@@ -633,19 +633,33 @@ class CatchWarningTests(BaseTest):
def test_check_warnings(self): def test_check_warnings(self):
# Explicit tests for the test_support convenience wrapper # Explicit tests for the test_support convenience wrapper
wmod = self.module wmod = self.module
if wmod is sys.modules['warnings']: if wmod is not sys.modules['warnings']:
with test_support.check_warnings() as w: return
self.assertEqual(w.warnings, []) with test_support.check_warnings(quiet=False) as w:
wmod.simplefilter("always") self.assertEqual(w.warnings, [])
wmod.warn("foo") wmod.simplefilter("always")
self.assertEqual(str(w.message), "foo") wmod.warn("foo")
wmod.warn("bar") self.assertEqual(str(w.message), "foo")
self.assertEqual(str(w.message), "bar") wmod.warn("bar")
self.assertEqual(str(w.warnings[0].message), "foo") self.assertEqual(str(w.message), "bar")
self.assertEqual(str(w.warnings[1].message), "bar") self.assertEqual(str(w.warnings[0].message), "foo")
w.reset() self.assertEqual(str(w.warnings[1].message), "bar")
self.assertEqual(w.warnings, []) w.reset()
self.assertEqual(w.warnings, [])
with test_support.check_warnings():
# defaults to quiet=True without argument
pass
with test_support.check_warnings(('foo', UserWarning)):
wmod.warn("foo")
with self.assertRaises(AssertionError):
with test_support.check_warnings(('', RuntimeWarning)):
# defaults to quiet=False with argument
pass
with self.assertRaises(AssertionError):
with test_support.check_warnings(('foo', RuntimeWarning)):
wmod.warn("foo")
class CCatchWarningTests(CatchWarningTests): class CCatchWarningTests(CatchWarningTests):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment