From cf95febe026ae347b365bdcc3dc0ddc19a9cb4e8 Mon Sep 17 00:00:00 2001 From: Ross Patterson Date: Mon, 12 Mar 2012 10:57:28 -0700 Subject: Switch to the warnings.catch_warnings context manager for testing. FYI, this test was failing only under 2.7, not 2.6. --- pyramid/tests/test_config/test_settings.py | 32 +++++++++++------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/pyramid/tests/test_config/test_settings.py b/pyramid/tests/test_config/test_settings.py index 8a385b8ab..0f5239915 100644 --- a/pyramid/tests/test_config/test_settings.py +++ b/pyramid/tests/test_config/test_settings.py @@ -48,20 +48,6 @@ class TestSettingsConfiguratorMixin(unittest.TestCase): self.assertEqual(settings['a'], 1) class TestSettings(unittest.TestCase): - def setUp(self): - self.warnings = [] - import warnings - warnings.old_showwarning = warnings.showwarning - warnings.showwarning = self._showwarning - - def tearDown(self): - del self.warnings - import warnings - warnings.showwarning = warnings.old_showwarning - - def _showwarning(self, message, category, filename, lineno, file=None, - line=None): - self.warnings.append(message) def _getTargetClass(self): from pyramid.config.settings import Settings @@ -74,14 +60,20 @@ class TestSettings(unittest.TestCase): return klass(d, _environ_=environ) def test_getattr_success(self): - settings = self._makeOne({'reload_templates':False}) - self.assertEqual(settings.reload_templates, False) - self.assertEqual(len(self.warnings), 1) + import warnings + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings('always') + settings = self._makeOne({'reload_templates':False}) + self.assertEqual(settings.reload_templates, False) + self.assertEqual(len(w), 1) def test_getattr_fail(self): - settings = self._makeOne({}) - self.assertRaises(AttributeError, settings.__getattr__, 'wontexist') - self.assertEqual(len(self.warnings), 0) + import warnings + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings('always') + settings = self._makeOne({}) + self.assertRaises(AttributeError, settings.__getattr__, 'wontexist') + self.assertEqual(len(w), 0) def test_getattr_raises_attribute_error(self): settings = self._makeOne() -- cgit v1.2.3 From 5e99a22964ea4fa42020c822d44a6e534cb30a38 Mon Sep 17 00:00:00 2001 From: Ross Patterson Date: Mon, 12 Mar 2012 11:17:52 -0700 Subject: Use the warnings.catch_warnings context manager in more places. Neither 2.7 nor 2.6 with "setup.py test" or "setup.py nosetests" show warnings that don't already appear for origin/master. FYI, under 2.6 with nosetests I see: nose/loader.py:303: DeprecationWarning: registerView --- pyramid/tests/test_config/test_init.py | 58 ++++++++++++++--------------- pyramid/tests/test_config/test_rendering.py | 37 ++++++++---------- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/pyramid/tests/test_config/test_init.py b/pyramid/tests/test_config/test_init.py index da331e5ee..37c3de275 100644 --- a/pyramid/tests/test_config/test_init.py +++ b/pyramid/tests/test_config/test_init.py @@ -1,4 +1,5 @@ import unittest +import warnings import os @@ -545,39 +546,33 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(utility, pyramid.tests.test_config) def test_setup_registry_renderer_globals_factory(self): - import warnings - warnings.filterwarnings('ignore') - try: - from pyramid.registry import Registry - from pyramid.interfaces import IRendererGlobalsFactory - reg = Registry() - config = self._makeOne(reg) - factory = object() + from pyramid.registry import Registry + from pyramid.interfaces import IRendererGlobalsFactory + reg = Registry() + config = self._makeOne(reg) + factory = object() + with warnings.catch_warnings(): + warnings.filterwarnings('ignore') config.setup_registry(renderer_globals_factory=factory) - self.assertEqual(reg.queryUtility(IRendererGlobalsFactory), None) - config.commit() - utility = reg.getUtility(IRendererGlobalsFactory) - self.assertEqual(utility, factory) - finally: - warnings.resetwarnings() + self.assertEqual(reg.queryUtility(IRendererGlobalsFactory), None) + config.commit() + utility = reg.getUtility(IRendererGlobalsFactory) + self.assertEqual(utility, factory) def test_setup_registry_renderer_globals_factory_dottedname(self): - import warnings - warnings.filterwarnings('ignore') - try: - from pyramid.registry import Registry - from pyramid.interfaces import IRendererGlobalsFactory - reg = Registry() - config = self._makeOne(reg) - import pyramid.tests.test_config + from pyramid.registry import Registry + from pyramid.interfaces import IRendererGlobalsFactory + reg = Registry() + config = self._makeOne(reg) + import pyramid.tests.test_config + with warnings.catch_warnings(): + warnings.filterwarnings('ignore') config.setup_registry( renderer_globals_factory='pyramid.tests.test_config') - self.assertEqual(reg.queryUtility(IRendererGlobalsFactory), None) - config.commit() - utility = reg.getUtility(IRendererGlobalsFactory) - self.assertEqual(utility, pyramid.tests.test_config) - finally: - warnings.resetwarnings() + self.assertEqual(reg.queryUtility(IRendererGlobalsFactory), None) + config.commit() + utility = reg.getUtility(IRendererGlobalsFactory) + self.assertEqual(utility, pyramid.tests.test_config) def test_setup_registry_alternate_renderers(self): from pyramid.registry import Registry @@ -1182,13 +1177,14 @@ pyramid.tests.test_config.dummy_include2""", self.assertTrue(getattr(foo_meth, im_func) is foo) class TestConfiguratorDeprecatedFeatures(unittest.TestCase): + def setUp(self): - import warnings + self.warnings = warnings.catch_warnings() + self.warnings.__enter__() warnings.filterwarnings('ignore') def tearDown(self): - import warnings - warnings.resetwarnings() + self.warnings.__exit__(None, None, None) def _makeOne(self, *arg, **kw): from pyramid.config import Configurator diff --git a/pyramid/tests/test_config/test_rendering.py b/pyramid/tests/test_config/test_rendering.py index 655735511..e6ee9ad70 100644 --- a/pyramid/tests/test_config/test_rendering.py +++ b/pyramid/tests/test_config/test_rendering.py @@ -1,4 +1,5 @@ import unittest +import warnings from pyramid.tests.test_config import dummyfactory @@ -9,32 +10,26 @@ class TestRenderingConfiguratorMixin(unittest.TestCase): return config def test_set_renderer_globals_factory(self): - import warnings - warnings.filterwarnings('ignore') - try: - from pyramid.interfaces import IRendererGlobalsFactory - config = self._makeOne(autocommit=True) - factory = object() + from pyramid.interfaces import IRendererGlobalsFactory + config = self._makeOne(autocommit=True) + factory = object() + with warnings.catch_warnings(): + warnings.filterwarnings('ignore') config.set_renderer_globals_factory(factory) - self.assertEqual( - config.registry.getUtility(IRendererGlobalsFactory), - factory) - finally: - warnings.resetwarnings() + self.assertEqual( + config.registry.getUtility(IRendererGlobalsFactory), + factory) def test_set_renderer_globals_factory_dottedname(self): - import warnings - warnings.filterwarnings('ignore') - try: - from pyramid.interfaces import IRendererGlobalsFactory - config = self._makeOne(autocommit=True) + from pyramid.interfaces import IRendererGlobalsFactory + config = self._makeOne(autocommit=True) + with warnings.catch_warnings(): + warnings.filterwarnings('ignore') config.set_renderer_globals_factory( 'pyramid.tests.test_config.dummyfactory') - self.assertEqual( - config.registry.getUtility(IRendererGlobalsFactory), - dummyfactory) - finally: - warnings.resetwarnings() + self.assertEqual( + config.registry.getUtility(IRendererGlobalsFactory), + dummyfactory) def test_add_renderer(self): from pyramid.interfaces import IRendererFactory -- cgit v1.2.3