Refs #21977 -- Removed SimpleTestCase.urls per deprecation timeline.

This commit is contained in:
Tim Graham 2015-08-18 12:07:13 -04:00
parent 5c62887d82
commit 1392aff440
4 changed files with 9 additions and 89 deletions

View File

@ -26,7 +26,6 @@ from django.core.management import call_command
from django.core.management.color import no_style from django.core.management.color import no_style
from django.core.management.sql import emit_post_migrate_signal from django.core.management.sql import emit_post_migrate_signal
from django.core.servers.basehttp import WSGIRequestHandler, WSGIServer from django.core.servers.basehttp import WSGIRequestHandler, WSGIServer
from django.core.urlresolvers import clear_url_caches, set_urlconf
from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction
from django.forms.fields import CharField from django.forms.fields import CharField
from django.http import QueryDict from django.http import QueryDict
@ -39,9 +38,7 @@ from django.test.utils import (
) )
from django.utils import six from django.utils import six
from django.utils.decorators import classproperty from django.utils.decorators import classproperty
from django.utils.deprecation import ( from django.utils.deprecation import RemovedInDjango20Warning
RemovedInDjango20Warning, RemovedInDjango110Warning,
)
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.six.moves.urllib.parse import ( from django.utils.six.moves.urllib.parse import (
unquote, urlparse, urlsplit, urlunsplit, unquote, urlparse, urlsplit, urlunsplit,
@ -227,33 +224,11 @@ class SimpleTestCase(unittest.TestCase):
* Clearing the mail test outbox. * Clearing the mail test outbox.
""" """
self.client = self.client_class() self.client = self.client_class()
self._urlconf_setup()
mail.outbox = [] mail.outbox = []
def _urlconf_setup(self):
if hasattr(self, 'urls'):
warnings.warn(
"SimpleTestCase.urls is deprecated and will be removed in "
"Django 1.10. Use @override_settings(ROOT_URLCONF=...) "
"in %s instead." % self.__class__.__name__,
RemovedInDjango110Warning, stacklevel=2)
set_urlconf(None)
self._old_root_urlconf = settings.ROOT_URLCONF
settings.ROOT_URLCONF = self.urls
clear_url_caches()
def _post_teardown(self): def _post_teardown(self):
"""Performs any post-test things. This includes: """Perform any post-test things."""
pass
* Putting back the original ROOT_URLCONF if it was changed.
"""
self._urlconf_teardown()
def _urlconf_teardown(self):
if hasattr(self, '_old_root_urlconf'):
set_urlconf(None)
settings.ROOT_URLCONF = self._old_root_urlconf
clear_url_caches()
def settings(self, **kwargs): def settings(self, **kwargs):
""" """

View File

@ -1307,10 +1307,10 @@ Built-in template context processors have been moved to
``django.test.SimpleTestCase.urls`` ``django.test.SimpleTestCase.urls``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The attribute :attr:`SimpleTestCase.urls <django.test.SimpleTestCase.urls>` The attribute ``SimpleTestCase.urls`` for specifying URLconf configuration in
for specifying URLconf configuration in tests has been deprecated and will be tests has been deprecated and will be removed in Django 1.10. Use
removed in Django 1.10. Use :func:`@override_settings(ROOT_URLCONF=...) :func:`@override_settings(ROOT_URLCONF=...) <django.test.override_settings>`
<django.test.override_settings>` instead. instead.
``prefix`` argument to :func:`~django.conf.urls.i18n.i18n_patterns` ``prefix`` argument to :func:`~django.conf.urls.i18n.i18n_patterns`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -639,7 +639,6 @@ functionality like:
* The ability to run tests with :ref:`modified settings <overriding-settings>`. * The ability to run tests with :ref:`modified settings <overriding-settings>`.
* Using the :attr:`~SimpleTestCase.client` :class:`~django.test.Client`. * Using the :attr:`~SimpleTestCase.client` :class:`~django.test.Client`.
* Custom test-time :attr:`URL maps <SimpleTestCase.urls>`.
If you need any of the other more complex and heavyweight Django-specific If you need any of the other more complex and heavyweight Django-specific
features like: features like:
@ -1080,39 +1079,12 @@ using multiple databases and set :attr:`multi_db=True
URLconf configuration URLconf configuration
~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
.. attribute:: SimpleTestCase.urls
.. deprecated:: 1.8
Use ``@override_settings(ROOT_URLCONF=...)`` instead for URLconf
configuration.
If your application provides views, you may want to include tests that use the If your application provides views, you may want to include tests that use the
test client to exercise those views. However, an end user is free to deploy the test client to exercise those views. However, an end user is free to deploy the
views in your application at any URL of their choosing. This means that your views in your application at any URL of their choosing. This means that your
tests can't rely upon the fact that your views will be available at a tests can't rely upon the fact that your views will be available at a
particular URL. particular URL. Decorate your test class or test method with
``@override_settings(ROOT_URLCONF=...)`` for URLconf configuration.
In order to provide a reliable URL space for your test,
``django.test.*TestCase`` classes provide the ability to customize the URLconf
configuration for the duration of the execution of a test suite. If your
``*TestCase`` instance defines an ``urls`` attribute, the ``*TestCase`` will use
the value of that attribute as the :setting:`ROOT_URLCONF` for the duration
of that test.
For example::
from django.test import TestCase
class TestMyViews(TestCase):
urls = 'myapp.test_urls'
def test_index_page_view(self):
# Here you'd test your view using ``Client``.
call_some_test_code()
This test case will use the contents of ``myapp.test_urls`` as the
URLconf for the duration of the test case.
.. _emptying-test-outbox: .. _emptying-test-outbox:

View File

@ -1,14 +1,11 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os
import unittest
import warnings import warnings
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.test.utils import reset_warning_registry from django.test.utils import reset_warning_registry
from django.utils import six from django.utils import six
from django.utils.deprecation import RenameMethodsBase from django.utils.deprecation import RenameMethodsBase
from django.utils.encoding import force_text
class RenameManagerMethods(RenameMethodsBase): class RenameManagerMethods(RenameMethodsBase):
@ -173,27 +170,3 @@ class RenameMethodsTests(SimpleTestCase):
'`DeprecatedMixin.old` is deprecated, use `new` instead.', '`DeprecatedMixin.old` is deprecated, use `new` instead.',
'`RenamedMixin.old` is deprecated, use `new` instead.', '`RenamedMixin.old` is deprecated, use `new` instead.',
]) ])
class DeprecatingSimpleTestCaseUrls(unittest.TestCase):
def test_deprecation(self):
"""
Ensure the correct warning is raised when SimpleTestCase.urls is used.
"""
class TempTestCase(SimpleTestCase):
urls = 'tests.urls'
def test(self):
pass
with warnings.catch_warnings(record=True) as recorded:
warnings.filterwarnings('always')
suite = unittest.TestLoader().loadTestsFromTestCase(TempTestCase)
with open(os.devnull, 'w') as devnull:
unittest.TextTestRunner(stream=devnull, verbosity=2).run(suite)
msg = force_text(recorded.pop().message)
self.assertEqual(msg,
"SimpleTestCase.urls is deprecated and will be removed in "
"Django 1.10. Use @override_settings(ROOT_URLCONF=...) "
"in TempTestCase instead.")