diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2008-07-13 12:23:47 +0000 |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2008-07-13 12:23:47 +0000 |
commit | 38469e271929399c12d6770d91958d8abac7433a (patch) | |
tree | b347e1d7a9b8176cae5bc57b7f9f5180c7438064 /Lib/test/test_warnings.py | |
parent | 3d0b9f095a1ccda7d6c04a9a1d05d245d8b82e26 (diff) | |
download | cpython-git-38469e271929399c12d6770d91958d8abac7433a.tar.gz |
Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Diffstat (limited to 'Lib/test/test_warnings.py')
-rw-r--r-- | Lib/test/test_warnings.py | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index ed498e00aa..7c1706aaa0 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -488,6 +488,49 @@ 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 test_support.catch_warning(wmod): + wmod.filters = wmod.showwarning = object() + self.assert_(wmod.filters is orig_filters) + self.assert_(wmod.showwarning is orig_showwarning) + with test_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 test_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 test_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 + + class ShowwarningDeprecationTests(BaseTest): """Test the deprecation of the old warnings.showwarning() API works.""" @@ -513,7 +556,6 @@ class PyShowwarningDeprecationTests(ShowwarningDeprecationTests): module = py_warnings - def test_main(): py_warnings.onceregistry.clear() c_warnings.onceregistry.clear() @@ -524,6 +566,7 @@ def test_main(): CWCmdLineTests, PyWCmdLineTests, _WarningsTests, CWarningsDisplayTests, PyWarningsDisplayTests, + CWarningsSupportTests, PyWarningsSupportTests, CShowwarningDeprecationTests, PyShowwarningDeprecationTests, ) |