summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_warnings.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2008-07-13 12:25:08 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2008-07-13 12:25:08 (GMT)
commitb130493834e5bd807ab80dcd8b417d7623baca24 (patch)
tree3842a93fe70989950ba9db64bc8d4a5543286e59 /Lib/test/test_warnings.py
parent628b1b3659d9db96210cb599579b79625eed1488 (diff)
downloadcpython-b130493834e5bd807ab80dcd8b417d7623baca24.zip
cpython-b130493834e5bd807ab80dcd8b417d7623baca24.tar.gz
cpython-b130493834e5bd807ab80dcd8b417d7623baca24.tar.bz2
Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for it to test_warnings. (forward port of r64910 from trunk)
Diffstat (limited to 'Lib/test/test_warnings.py')
-rw-r--r--Lib/test/test_warnings.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
index 1e17313..b6634c0 100644
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -487,6 +487,47 @@ class CWarningsDisplayTests(BaseTest, WarningsDisplayTests):
class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests):
module = py_warnings
+class WarningsSupportTests(object):
+ """Test the warning tools from test support module"""
+
+ def test_catch_warning_restore(self):
+ wmod = self.module
+ orig_filters = wmod.filters
+ orig_showwarning = wmod.showwarning
+ with support.catch_warning(wmod):
+ wmod.filters = wmod.showwarning = object()
+ self.assert_(wmod.filters is orig_filters)
+ self.assert_(wmod.showwarning is orig_showwarning)
+ with support.catch_warning(wmod, record=False):
+ wmod.filters = wmod.showwarning = object()
+ self.assert_(wmod.filters is orig_filters)
+ self.assert_(wmod.showwarning is orig_showwarning)
+
+ def test_catch_warning_recording(self):
+ wmod = self.module
+ with support.catch_warning(wmod) as w:
+ self.assertEqual(w.warnings, [])
+ wmod.simplefilter("always")
+ wmod.warn("foo")
+ self.assertEqual(str(w.message), "foo")
+ wmod.warn("bar")
+ self.assertEqual(str(w.message), "bar")
+ self.assertEqual(str(w.warnings[0].message), "foo")
+ self.assertEqual(str(w.warnings[1].message), "bar")
+ w.reset()
+ self.assertEqual(w.warnings, [])
+ orig_showwarning = wmod.showwarning
+ with support.catch_warning(wmod, record=False) as w:
+ self.assert_(w is None)
+ self.assert_(wmod.showwarning is orig_showwarning)
+
+
+class CWarningsSupportTests(BaseTest, WarningsSupportTests):
+ module = c_warnings
+
+class PyWarningsSupportTests(BaseTest, WarningsSupportTests):
+ module = py_warnings
+
def test_main():
py_warnings.onceregistry.clear()
@@ -498,6 +539,7 @@ def test_main():
CWCmdLineTests, PyWCmdLineTests,
_WarningsTests,
CWarningsDisplayTests, PyWarningsDisplayTests,
+ CWarningsSupportTests, PyWarningsSupportTests,
)