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 /django | |
| 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 'django')
| -rw-r--r-- | django/contrib/auth/tests/auth_backends.py | 4 | ||||
| -rw-r--r-- | django/contrib/messages/tests/base.py | 4 | ||||
| -rw-r--r-- | django/test/testcases.py | 14 | ||||
| -rw-r--r-- | django/test/utils.py | 22 |
4 files changed, 40 insertions, 4 deletions
diff --git a/django/contrib/auth/tests/auth_backends.py b/django/contrib/auth/tests/auth_backends.py index 2ba8a73639..5dbfab12c1 100644 --- a/django/contrib/auth/tests/auth_backends.py +++ b/django/contrib/auth/tests/auth_backends.py @@ -154,13 +154,13 @@ class RowlevelBackendTest(TestCase): self.user1 = User.objects.create_user('test', 'test@example.com', 'test') self.user2 = User.objects.create_user('test2', 'test2@example.com', 'test') self.user3 = User.objects.create_user('test3', 'test3@example.com', 'test') + self.save_warnings_state() warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.contrib.auth') def tearDown(self): settings.AUTHENTICATION_BACKENDS = self.curr_auth - warnings.resetwarnings() - warnings.simplefilter('ignore', PendingDeprecationWarning) + self.restore_warnings_state() def test_has_perm(self): self.assertEqual(self.user1.has_perm('perm', TestObj()), False) diff --git a/django/contrib/messages/tests/base.py b/django/contrib/messages/tests/base.py index 1c42c1d0a3..9c6132bf3b 100644 --- a/django/contrib/messages/tests/base.py +++ b/django/contrib/messages/tests/base.py @@ -51,6 +51,7 @@ class BaseTest(TestCase): self._message_storage = settings.MESSAGE_STORAGE settings.MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__, self.storage_class.__name__) + self.save_warnings_state() warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.contrib.auth.models') @@ -63,8 +64,7 @@ class BaseTest(TestCase): self._template_context_processors settings.INSTALLED_APPS = self._installed_apps settings.MESSAGE_STORAGE = self._message_storage - warnings.resetwarnings() - warnings.simplefilter('ignore', PendingDeprecationWarning) + self.restore_warnings_state() def restore_setting(self, setting): if setting in self._remembered_settings: diff --git a/django/test/testcases.py b/django/test/testcases.py index 57472f7631..398ad774d8 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -11,6 +11,7 @@ from django.db import transaction, connection, connections, DEFAULT_DB_ALIAS from django.http import QueryDict from django.test import _doctest as doctest from django.test.client import Client +from django.test.utils import get_warnings_state, restore_warnings_state from django.utils import simplejson, unittest as ut2 from django.utils.encoding import smart_str from django.utils.functional import wraps @@ -328,6 +329,19 @@ class TransactionTestCase(ut2.TestCase): settings.ROOT_URLCONF = self._old_root_urlconf clear_url_caches() + def save_warnings_state(self): + """ + Saves the state of the warnings module + """ + self._warnings_state = get_warnings_state() + + def restore_warnings_state(self): + """ + Restores the sate of the warnings module to the state + saved by save_warnings_state() + """ + restore_warnings_state(self._warnings_state) + def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200, host=None, msg_prefix=''): """Asserts that a response redirected to a specific URL, and that the diff --git a/django/test/utils.py b/django/test/utils.py index 063e30297f..90a8b3e88f 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -1,6 +1,7 @@ import sys import time import os +import warnings from django.conf import settings from django.core import mail from django.core.mail.backends import locmem @@ -46,6 +47,7 @@ class ContextList(list): return False return True + def instrumented_test_render(self, context): """ An instrumented Template render method, providing a signal @@ -75,6 +77,7 @@ def setup_test_environment(): deactivate() + def teardown_test_environment(): """Perform any global post-test teardown. This involves: @@ -93,6 +96,25 @@ def teardown_test_environment(): del mail.outbox + +def get_warnings_state(): + """ + Returns an object containing the state of the warnings module + """ + # There is no public interface for doing this, but this implementation of + # get_warnings_state and restore_warnings_state appears to work on Python + # 2.4 to 2.7. + return warnings.filters[:] + + +def restore_warnings_state(state): + """ + Restores the state of the warnings module when passed an object that was + returned by get_warnings_state() + """ + warnings.filters = state[:] + + def get_runner(settings): test_path = settings.TEST_RUNNER.split('.') # Allow for Python 2.5 relative paths |
