diff options
| author | Claude Paroz <claude@2xlibre.net> | 2014-12-21 21:13:06 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2014-12-30 18:16:25 +0100 |
| commit | 66f9a74b4514bd259976ce8ee3a4e78288358a5f (patch) | |
| tree | 4a3f5c24b15f53e9a886788110c735e0822783f9 /django/test | |
| parent | 8082c75d188e395c973f52bfdf5d547c741edd03 (diff) | |
Added ignore_warnings decorator
And removed Ignore*DeprecationWarningsMixin, now obsolete.
Thanks Berker Peksag and Tim Graham for the review.
Diffstat (limited to 'django/test')
| -rw-r--r-- | django/test/__init__.py | 7 | ||||
| -rw-r--r-- | django/test/utils.py | 49 |
2 files changed, 35 insertions, 21 deletions
diff --git a/django/test/__init__.py b/django/test/__init__.py index b65601b12d..f5213301d3 100644 --- a/django/test/__init__.py +++ b/django/test/__init__.py @@ -8,13 +8,14 @@ from django.test.testcases import ( SimpleTestCase, LiveServerTestCase, skipIfDBFeature, skipUnlessDBFeature ) -from django.test.utils import modify_settings, override_settings, override_system_checks +from django.test.utils import (ignore_warnings, modify_settings, + override_settings, override_system_checks) __all__ = [ 'Client', 'RequestFactory', 'TestCase', 'TransactionTestCase', 'SimpleTestCase', 'LiveServerTestCase', 'skipIfDBFeature', - 'skipUnlessDBFeature', 'modify_settings', 'override_settings', - 'override_system_checks' + 'skipUnlessDBFeature', 'ignore_warnings', 'modify_settings', + 'override_settings', 'override_system_checks' ] # To simplify Django's test suite; not meant as a public API diff --git a/django/test/utils.py b/django/test/utils.py index 961fece55c..2bc6cf3451 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -18,7 +18,7 @@ from django.template import Template from django.template.loaders import locmem from django.test.signals import template_rendered, setting_changed from django.utils import six -from django.utils.deprecation import RemovedInDjango19Warning, RemovedInDjango20Warning +from django.utils.deprecation import RemovedInDjango19Warning from django.utils.encoding import force_str from django.utils.translation import deactivate @@ -432,27 +432,40 @@ class CaptureQueriesContext(object): self.final_queries = len(self.connection.queries_log) -class IgnoreDeprecationWarningsMixin(object): - warning_classes = [RemovedInDjango19Warning] - - def setUp(self): - super(IgnoreDeprecationWarningsMixin, self).setUp() - self.catch_warnings = warnings.catch_warnings() - self.catch_warnings.__enter__() - for warning_class in self.warning_classes: - warnings.filterwarnings("ignore", category=warning_class) - - def tearDown(self): - self.catch_warnings.__exit__(*sys.exc_info()) - super(IgnoreDeprecationWarningsMixin, self).tearDown() +class ignore_warnings(object): + def __init__(self, **kwargs): + self.ignore_kwargs = kwargs + if 'message' in self.ignore_kwargs or 'module' in self.ignore_kwargs: + self.filter_func = warnings.filterwarnings + else: + self.filter_func = warnings.simplefilter + def __call__(self, decorated): + if isinstance(decorated, type): + # A class is decorated + saved_setUp = decorated.setUp + saved_tearDown = decorated.tearDown -class IgnorePendingDeprecationWarningsMixin(IgnoreDeprecationWarningsMixin): - warning_classes = [RemovedInDjango20Warning] + def setUp(inner_self): + self.catch_warnings = warnings.catch_warnings() + self.catch_warnings.__enter__() + self.filter_func('ignore', **self.ignore_kwargs) + saved_setUp(inner_self) + def tearDown(inner_self): + saved_tearDown(inner_self) + self.catch_warnings.__exit__(*sys.exc_info()) -class IgnoreAllDeprecationWarningsMixin(IgnoreDeprecationWarningsMixin): - warning_classes = [RemovedInDjango20Warning, RemovedInDjango19Warning] + decorated.setUp = setUp + decorated.tearDown = tearDown + return decorated + else: + @wraps(decorated) + def inner(*args, **kwargs): + with warnings.catch_warnings(): + self.filter_func('ignore', **self.ignore_kwargs) + return decorated(*args, **kwargs) + return inner @contextmanager |
