mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Deprecated undocumented warnings manipulation testing tools.
This commit is contained in:
parent
9a4a1ce323
commit
7947c9e3a6
@ -1,12 +1,13 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from copy import copy
|
||||||
import difflib
|
import difflib
|
||||||
|
import errno
|
||||||
|
from functools import wraps
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from copy import copy
|
|
||||||
from functools import wraps
|
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlsplit, urlunsplit
|
from urllib.parse import urlsplit, urlunsplit
|
||||||
except ImportError: # Python 2
|
except ImportError: # Python 2
|
||||||
@ -14,7 +15,7 @@ except ImportError: # Python 2
|
|||||||
import select
|
import select
|
||||||
import socket
|
import socket
|
||||||
import threading
|
import threading
|
||||||
import errno
|
import warnings
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.staticfiles.handlers import StaticFilesHandler
|
from django.contrib.staticfiles.handlers import StaticFilesHandler
|
||||||
@ -36,8 +37,7 @@ from django.test import _doctest as doctest
|
|||||||
from django.test.client import Client
|
from django.test.client import Client
|
||||||
from django.test.html import HTMLParseError, parse_html
|
from django.test.html import HTMLParseError, parse_html
|
||||||
from django.test.signals import template_rendered
|
from django.test.signals import template_rendered
|
||||||
from django.test.utils import (get_warnings_state, restore_warnings_state,
|
from django.test.utils import (override_settings, compare_xml, strip_quotes)
|
||||||
override_settings, compare_xml, strip_quotes)
|
|
||||||
from django.test.utils import ContextList
|
from django.test.utils import ContextList
|
||||||
from django.utils import unittest as ut2
|
from django.utils import unittest as ut2
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
@ -241,6 +241,11 @@ class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext):
|
|||||||
|
|
||||||
|
|
||||||
class SimpleTestCase(ut2.TestCase):
|
class SimpleTestCase(ut2.TestCase):
|
||||||
|
|
||||||
|
_warn_txt = ("save_warnings_state/restore_warnings_state "
|
||||||
|
"django.test.*TestCase methods are deprecated. Use Python's "
|
||||||
|
"warnings.catch_warnings context manager instead.")
|
||||||
|
|
||||||
def __call__(self, result=None):
|
def __call__(self, result=None):
|
||||||
"""
|
"""
|
||||||
Wrapper around default __call__ method to perform common Django test
|
Wrapper around default __call__ method to perform common Django test
|
||||||
@ -279,14 +284,16 @@ class SimpleTestCase(ut2.TestCase):
|
|||||||
"""
|
"""
|
||||||
Saves the state of the warnings module
|
Saves the state of the warnings module
|
||||||
"""
|
"""
|
||||||
self._warnings_state = get_warnings_state()
|
warnings.warn(self._warn_txt, DeprecationWarning, stacklevel=2)
|
||||||
|
self._warnings_state = warnings.filters[:]
|
||||||
|
|
||||||
def restore_warnings_state(self):
|
def restore_warnings_state(self):
|
||||||
"""
|
"""
|
||||||
Restores the state of the warnings module to the state
|
Restores the state of the warnings module to the state
|
||||||
saved by save_warnings_state()
|
saved by save_warnings_state()
|
||||||
"""
|
"""
|
||||||
restore_warnings_state(self._warnings_state)
|
warnings.warn(self._warn_txt, DeprecationWarning, stacklevel=2)
|
||||||
|
warnings.filters = self._warnings_state[:]
|
||||||
|
|
||||||
def settings(self, **kwargs):
|
def settings(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -98,6 +98,11 @@ def teardown_test_environment():
|
|||||||
del mail.outbox
|
del mail.outbox
|
||||||
|
|
||||||
|
|
||||||
|
warn_txt = ("get_warnings_state/restore_warnings_state functions from "
|
||||||
|
"django.test.utils are deprecated. Use Python's warnings.catch_warnings() "
|
||||||
|
"context manager instead.")
|
||||||
|
|
||||||
|
|
||||||
def get_warnings_state():
|
def get_warnings_state():
|
||||||
"""
|
"""
|
||||||
Returns an object containing the state of the warnings module
|
Returns an object containing the state of the warnings module
|
||||||
@ -105,6 +110,7 @@ def get_warnings_state():
|
|||||||
# There is no public interface for doing this, but this implementation of
|
# There is no public interface for doing this, but this implementation of
|
||||||
# get_warnings_state and restore_warnings_state appears to work on Python
|
# get_warnings_state and restore_warnings_state appears to work on Python
|
||||||
# 2.4 to 2.7.
|
# 2.4 to 2.7.
|
||||||
|
warnings.warn(warn_txt, DeprecationWarning, stacklevel=2)
|
||||||
return warnings.filters[:]
|
return warnings.filters[:]
|
||||||
|
|
||||||
|
|
||||||
@ -113,6 +119,7 @@ def restore_warnings_state(state):
|
|||||||
Restores the state of the warnings module when passed an object that was
|
Restores the state of the warnings module when passed an object that was
|
||||||
returned by get_warnings_state()
|
returned by get_warnings_state()
|
||||||
"""
|
"""
|
||||||
|
warnings.warn(warn_txt, DeprecationWarning, stacklevel=2)
|
||||||
warnings.filters = state[:]
|
warnings.filters = state[:]
|
||||||
|
|
||||||
|
|
||||||
|
@ -267,7 +267,6 @@ these changes.
|
|||||||
in 1.4. The backward compatibility will be removed --
|
in 1.4. The backward compatibility will be removed --
|
||||||
``HttpRequest.raw_post_data`` will no longer work.
|
``HttpRequest.raw_post_data`` will no longer work.
|
||||||
|
|
||||||
|
|
||||||
* The value for the ``post_url_continue`` parameter in
|
* The value for the ``post_url_continue`` parameter in
|
||||||
``ModelAdmin.response_add()`` will have to be either ``None`` (to redirect
|
``ModelAdmin.response_add()`` will have to be either ``None`` (to redirect
|
||||||
to the newly created object's edit page) or a pre-formatted url. String
|
to the newly created object's edit page) or a pre-formatted url. String
|
||||||
@ -314,6 +313,13 @@ these changes.
|
|||||||
* The ``depth`` keyword argument will be removed from
|
* The ``depth`` keyword argument will be removed from
|
||||||
:meth:`~django.db.models.query.QuerySet.select_related`.
|
:meth:`~django.db.models.query.QuerySet.select_related`.
|
||||||
|
|
||||||
|
* The undocumented ``get_warnings_state()``/``restore_warnings_state()``
|
||||||
|
functions from :mod:`django.test.utils` and the ``save_warnings_state()``/
|
||||||
|
``restore_warnings_state()``
|
||||||
|
:ref:`django.test.*TestCase <django-testcase-subclasses>` methods are
|
||||||
|
deprecated. Use the :class:`warnings.catch_warnings` context manager
|
||||||
|
available starting with Python 2.6 instead.
|
||||||
|
|
||||||
1.8
|
1.8
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -835,6 +835,8 @@ The following is a simple unit test using the test client::
|
|||||||
|
|
||||||
:class:`django.test.client.RequestFactory`
|
:class:`django.test.client.RequestFactory`
|
||||||
|
|
||||||
|
.. _django-testcase-subclasses:
|
||||||
|
|
||||||
Provided test case classes
|
Provided test case classes
|
||||||
--------------------------
|
--------------------------
|
||||||
|
|
||||||
|
@ -220,24 +220,27 @@ class SaveRestoreWarningState(TestCase):
|
|||||||
# of save_warnings_state/restore_warnings_state (e.g. just
|
# of save_warnings_state/restore_warnings_state (e.g. just
|
||||||
# warnings.resetwarnings()) , but it is difficult to test more.
|
# warnings.resetwarnings()) , but it is difficult to test more.
|
||||||
import warnings
|
import warnings
|
||||||
self.save_warnings_state()
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("ignore", DeprecationWarning)
|
||||||
|
|
||||||
class MyWarning(Warning):
|
self.save_warnings_state()
|
||||||
pass
|
|
||||||
|
|
||||||
# Add a filter that causes an exception to be thrown, so we can catch it
|
class MyWarning(Warning):
|
||||||
warnings.simplefilter("error", MyWarning)
|
pass
|
||||||
self.assertRaises(Warning, lambda: warnings.warn("warn", MyWarning))
|
|
||||||
|
|
||||||
# Now restore.
|
# Add a filter that causes an exception to be thrown, so we can catch it
|
||||||
self.restore_warnings_state()
|
warnings.simplefilter("error", MyWarning)
|
||||||
# After restoring, we shouldn't get an exception. But we don't want a
|
self.assertRaises(Warning, lambda: warnings.warn("warn", MyWarning))
|
||||||
# warning printed either, so we have to silence the warning.
|
|
||||||
warnings.simplefilter("ignore", MyWarning)
|
|
||||||
warnings.warn("warn", MyWarning)
|
|
||||||
|
|
||||||
# Remove the filter we just added.
|
# Now restore.
|
||||||
self.restore_warnings_state()
|
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()
|
||||||
|
|
||||||
|
|
||||||
class HTMLEqualTests(TestCase):
|
class HTMLEqualTests(TestCase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user