diff options
| author | Luke Plant <L.Plant.98@cantab.net> | 2010-11-11 15:06:20 +0000 |
|---|---|---|
| committer | Luke Plant <L.Plant.98@cantab.net> | 2010-11-11 15:06:20 +0000 |
| commit | 02fc6276d7577dcec26be30f3805b242ff0ce8a0 (patch) | |
| tree | a85f54ee41330e4289169d7cac4ee04b55e22ba6 /tests/regressiontests | |
| parent | 7beca4d3e5da784297ff7163d00dcfeee9a34dd6 (diff) | |
Fixed #14508 - test suite silences warnings.
Utility functions get_warnings_state and save_warnings_state have been added
to django.test.utils, and methods to django.test.TestCase for convenience.
The implementation is based on the catch_warnings context manager from
Python 2.6.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14526 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
| -rw-r--r-- | tests/regressiontests/cache/tests.py | 22 | ||||
| -rw-r--r-- | tests/regressiontests/context_processors/tests.py | 4 | ||||
| -rw-r--r-- | tests/regressiontests/csrf_tests/tests.py | 4 | ||||
| -rw-r--r-- | tests/regressiontests/syndication/tests.py | 4 | ||||
| -rw-r--r-- | tests/regressiontests/templates/loaders.py | 5 | ||||
| -rw-r--r-- | tests/regressiontests/test_utils/tests.py | 29 |
6 files changed, 47 insertions, 21 deletions
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index da4c9a8e7a..575021dc2b 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -15,6 +15,7 @@ from django.core.cache import get_cache from django.core.cache.backends.base import InvalidCacheBackendError, CacheKeyWarning from django.http import HttpResponse, HttpRequest from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware +from django.test.utils import get_warnings_state, restore_warnings_state from django.utils import translation from django.utils import unittest from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key @@ -379,21 +380,16 @@ class BaseCacheTests(object): # manager to test this warning nicely. Since we can't do that # yet, the cleanest option is to temporarily ask for # CacheKeyWarning to be raised as an exception. + _warnings_state = get_warnings_state() warnings.simplefilter("error", CacheKeyWarning) - # memcached does not allow whitespace or control characters in keys - self.assertRaises(CacheKeyWarning, self.cache.set, 'key with spaces', 'value') - # memcached limits key length to 250 - self.assertRaises(CacheKeyWarning, self.cache.set, 'a' * 251, 'value') - - # The warnings module has no public API for getting the - # current list of warning filters, so we can't save that off - # and reset to the previous value, we have to globally reset - # it. The effect will be the same, as long as the Django test - # runner doesn't add any global warning filters (it currently - # does not). - warnings.resetwarnings() - warnings.simplefilter("ignore", PendingDeprecationWarning) + try: + # memcached does not allow whitespace or control characters in keys + self.assertRaises(CacheKeyWarning, self.cache.set, 'key with spaces', 'value') + # memcached limits key length to 250 + self.assertRaises(CacheKeyWarning, self.cache.set, 'a' * 251, 'value') + finally: + restore_warnings_state(_warnings_state) class DBCacheTests(unittest.TestCase, BaseCacheTests): def setUp(self): diff --git a/tests/regressiontests/context_processors/tests.py b/tests/regressiontests/context_processors/tests.py index 2b5ddbe94c..5a89561bb6 100644 --- a/tests/regressiontests/context_processors/tests.py +++ b/tests/regressiontests/context_processors/tests.py @@ -48,14 +48,14 @@ class AuthContextProcessorTests(TestCase): fixtures = ['context-processors-users.xml'] def setUp(self): + self.save_warnings_state() warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.contrib.auth.models') warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.core.context_processors') def tearDown(self): - warnings.resetwarnings() - warnings.simplefilter('ignore', PendingDeprecationWarning) + self.restore_warnings_state() def test_session_not_accessed(self): """ diff --git a/tests/regressiontests/csrf_tests/tests.py b/tests/regressiontests/csrf_tests/tests.py index 7265b5b788..ea18d7d010 100644 --- a/tests/regressiontests/csrf_tests/tests.py +++ b/tests/regressiontests/csrf_tests/tests.py @@ -71,12 +71,12 @@ class CsrfMiddlewareTest(TestCase): _secret_key_for_session_test= "test" def setUp(self): + self.save_warnings_state() warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.middleware.csrf') def tearDown(self): - warnings.resetwarnings() - warnings.simplefilter('ignore', PendingDeprecationWarning) + self.restore_warnings_state() def _get_GET_no_csrf_cookie_request(self): return TestingHttpRequest() diff --git a/tests/regressiontests/syndication/tests.py b/tests/regressiontests/syndication/tests.py index a69a454921..b9d77d9b0b 100644 --- a/tests/regressiontests/syndication/tests.py +++ b/tests/regressiontests/syndication/tests.py @@ -315,14 +315,14 @@ class DeprecatedSyndicationFeedTest(FeedTestCase): Tests for the deprecated API (feed() view and the feed_dict etc). """ def setUp(self): + self.save_warnings_state() warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.contrib.syndication.feeds') warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.contrib.syndication.views') def tearDown(self): - warnings.resetwarnings() - warnings.simplefilter('ignore', PendingDeprecationWarning) + self.restore_warnings_state() def test_empty_feed_dict(self): """ diff --git a/tests/regressiontests/templates/loaders.py b/tests/regressiontests/templates/loaders.py index c59f838b4b..f0759ec2c6 100644 --- a/tests/regressiontests/templates/loaders.py +++ b/tests/regressiontests/templates/loaders.py @@ -20,6 +20,7 @@ from django.template import TemplateDoesNotExist, Context from django.template.loaders.eggs import load_template_source as lts_egg from django.template.loaders.eggs import Loader as EggLoader from django.template import loader +from django.test.utils import get_warnings_state, restore_warnings_state from django.utils import unittest @@ -68,13 +69,13 @@ class DeprecatedEggLoaderTest(unittest.TestCase): }) self._old_installed_apps = settings.INSTALLED_APPS settings.INSTALLED_APPS = [] + self._warnings_state = get_warnings_state() warnings.filterwarnings("ignore", category=DeprecationWarning, module='django.template.loaders.eggs') def tearDown(self): settings.INSTALLED_APPS = self._old_installed_apps - warnings.resetwarnings() - warnings.simplefilter("ignore", PendingDeprecationWarning) + restore_warnings_state(self._warnings_state) def test_existing(self): "A template can be loaded from an egg" diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py index 995306e01d..2e55ba7718 100644 --- a/tests/regressiontests/test_utils/tests.py +++ b/tests/regressiontests/test_utils/tests.py @@ -26,6 +26,35 @@ class SkippingTestCase(TestCase): self.assertRaises(ValueError, test_func) +class SaveRestoreWarningState(TestCase): + + def test_save_restore_warnings_state(self): + """ + Ensure save_warnings_state/restore_warnings_state work correctly. + """ + # In reality this test could be satisfied by many broken implementations + # of save_warnings_state/restore_warnings_state (e.g. just + # warnings.resetwarnings()) , but it is difficult to test more. + import warnings + self.save_warnings_state() + + class MyWarning(Warning): + pass + + # Add a filter that causes an exception to be thrown, so we can catch it + warnings.simplefilter("error", MyWarning) + self.assertRaises(Warning, lambda: warnings.warn("warn", MyWarning)) + + # Now restore. + self.restore_warnings_state() + # After restoring, we shouldn't get an exception. But we don't want a + # warning printed either, so we have to silence the warning. + warnings.simplefilter("ignore", MyWarning) + warnings.warn("warn", MyWarning) + + # Remove the filter we just added. + self.restore_warnings_state() + __test__ = {"API_TEST": r""" # Some checks of the doctest output normalizer. # Standard doctests do fairly |
