summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLuke Plant <L.Plant.98@cantab.net>2010-11-11 15:30:13 +0000
committerLuke Plant <L.Plant.98@cantab.net>2010-11-11 15:30:13 +0000
commitd7b8d07cf21ca058251a5e21a7d0dd356aefe00c (patch)
treecf2460da669debed6c15f8fb2282d91a8fda0896 /tests
parent79ab8cad03847146029f9f686aedfbdc8168807d (diff)
[1.2.X] 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. Backport of [14526] from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14527 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/cache/tests.py21
-rw-r--r--tests/regressiontests/templates/loaders.py4
2 files changed, 12 insertions, 13 deletions
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
index 1e0a4046bb..7167e2fbf8 100644
--- a/tests/regressiontests/cache/tests.py
+++ b/tests/regressiontests/cache/tests.py
@@ -16,6 +16,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.cache import patch_vary_headers, get_cache_key, learn_cache_key
from django.utils.hashcompat import md5_constructor
@@ -379,20 +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()
+ 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/templates/loaders.py b/tests/regressiontests/templates/loaders.py
index caa3faa6bc..47cd18ae21 100644
--- a/tests/regressiontests/templates/loaders.py
+++ b/tests/regressiontests/templates/loaders.py
@@ -21,6 +21,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
# Mock classes and objects for pkg_resources functions.
class MockProvider(pkg_resources.NullProvider):
@@ -67,11 +68,12 @@ class DeprecatedEggLoaderTest(unittest.TestCase):
})
self._old_installed_apps = settings.INSTALLED_APPS
settings.INSTALLED_APPS = []
+ self._warnings_state = get_warnings_state()
warnings.simplefilter("ignore", PendingDeprecationWarning)
def tearDown(self):
settings.INSTALLED_APPS = self._old_installed_apps
- warnings.resetwarnings()
+ restore_warnings_state(self._warnings_state)
def test_existing(self):
"A template can be loaded from an egg"