Ticket #11077: test_combine_urlconfs_docs.patch
File test_combine_urlconfs_docs.patch, 6.1 KB (added by , 15 years ago) |
---|
-
django/test/testcases.py
1 1 import re 2 import sys 3 import imp 2 4 import unittest 3 5 from urlparse import urlsplit, urlunsplit 4 6 from xml.dom.minidom import parseString, Node … … 13 15 from django.test.client import Client 14 16 from django.utils import simplejson 15 17 from django.utils.encoding import smart_str 18 from django.utils.importlib import import_module 16 19 17 20 try: 18 21 all … … 242 245 def _urlconf_setup(self): 243 246 if hasattr(self, 'urls'): 244 247 self._old_root_urlconf = settings.ROOT_URLCONF 245 settings.ROOT_URLCONF = self.urls 248 if settings.TEST_COMBINE_URLCONFS: 249 # Put the module instance of self.urls into specified_urlconf 250 if not isinstance(self.urls, basestring): 251 specified_urlconf = self.urls 252 else: 253 specified_urlconf = import_module(self.urls) 254 # Choose between settings.TEST_COMBINE_URLCONFS_WITH and settings.ROOT_URLCONF 255 urls_to_combine = settings.TEST_COMBINE_URLCONFS_WITH or settings.ROOT_URLCONF 256 # Do the above action, this time with urls_to_combine 257 if not isinstance(urls_to_combine, basestring): 258 extra_urlconf = urls_to_combine 259 else: 260 extra_urlconf = import_module(urls_to_combine) 261 # Create a dynamic URLconf module that combines the urlpatterns 262 # of specified_urlconf and extra_urlconf 263 dynamic_urlconf_name = 'dynamic_urls' 264 dynamic_urlconf = imp.new_module(dynamic_urlconf_name) 265 dynamic_urlconf.urlpatterns = specified_urlconf.urlpatterns + \ 266 extra_urlconf.urlpatterns 267 # Also combine handler404 and 500, favoring specified_urlconf 268 handler404 = getattr(specified_urlconf, 'handler404', None) or \ 269 getattr(extra_urlconf, 'handler404', None) 270 if handler404: 271 dynamic_urlconf.handler404 = handler404 272 handler500 = getattr(specified_urlconf, 'handler404', None) or \ 273 getattr(extra_urlconf, 'handler404', None) 274 if handler500: 275 dynamic_urlconf.handler500 = handler500 276 # Add our dynamic_urlconf to sys.modules and set 277 # settings.ROOT_URLCONF to it 278 sys.modules[dynamic_urlconf_name] = dynamic_urlconf 279 settings.ROOT_URLCONF = dynamic_urlconf 280 else: 281 settings.ROOT_URLCONF = self.urls 246 282 clear_url_caches() 247 283 248 284 def __call__(self, result=None): -
django/conf/global_settings.py
501 501 TEST_DATABASE_CHARSET = None 502 502 TEST_DATABASE_COLLATION = None 503 503 504 # If true, the testing framework will combine the URLconf instead of overwrite 505 # it if a TestCase.urls is specified 506 TEST_COMBINE_URLCONFS = False 507 508 # The URLconf to be combined with the TestCase.urls setting. If None, the 509 # ROOT_URLCONF specified in settings.py will be used instead. 510 TEST_COMBINE_URLCONFS_WITH = None 511 504 512 ############ 505 513 # FIXTURES # 506 514 ############ -
docs/topics/testing.txt
1054 1054 This test case will use the contents of ``myapp.test_urls`` as the 1055 1055 URLconf for the duration of the test case. 1056 1056 1057 The use of ``TestCase.urls`` has a downside, though. Since it overrides 1058 ``ROOT_URLCONF``, your project's other urls will not be accessible. This is 1059 normally not a problem, but custom templates with reverses to external apps will 1060 cause the tests to raise ``NoReverseMatch`` exceptions. This problem can happen 1061 with :mod:`django.contrib.auth` and other apps that support custom templates and 1062 set ``TestCase.urls``. Custom templates that include a ``base.html`` with links to 1063 various places around the site are common sources of this problem. 1064 1065 This situation can be fixed by combining the ``TestCase.urls`` with your 1066 project's ``ROOT_URLCONF``. You can also choose to combine a custom URLconf 1067 designed specifically for testing. See :setting:`TEST_COMBINE_URLCONFS` and 1068 :setting:`TEST_COMBINE_URLCONFS_WITH` for more information. 1069 1057 1070 .. _emptying-test-outbox: 1058 1071 1059 1072 Multi-database support -
docs/ref/settings.txt
1404 1404 Output, as a string, that the template system should use for invalid (e.g. 1405 1405 misspelled) variables. See :ref:`invalid-template-variables`.. 1406 1406 1407 .. setting:: TEST_COMBINE_URLCONFS 1408 1409 TEST_COMBINE_URLCONFS 1410 --------------------- 1411 1412 Default: ``False`` 1413 1414 If ``True``, the testing framework will combine the URLconf instead of overwrite 1415 it if a ``TestCase.urls`` is specified. The URLconf merged into the 1416 ``TestCase.urls`` will be :setting:`ROOT_URLCONF` unless 1417 :setting:`TEST_COMBINE_URLCONFS_WITH` is set. 1418 1419 This setting is useful for cases where apps like :mod:`django.contrib.auth` set 1420 ``TestCase.urls`` to override the :setting:`ROOT_URLCONF` with a custom testing 1421 URLconf. This setting will fix the problem where custom templates with reverses 1422 to external apps cause the tests to fail with ``NoReverseMatch`` errors. 1423 1424 .. setting:: TEST_COMBINE_URLCONFS_WITH 1425 1426 TEST_COMBINE_URLCONFS_WITH 1427 -------------------------- 1428 1429 Default: ``None`` 1430 1431 The URLconf to be combined with the ``TestCase.urls`` setting if it is set. If 1432 ``None``, the :setting:`ROOT_URLCONF` will be used instead. This setting will 1433 only be used if :setting:`TEST_COMBINE_URLCONFS` is ``True``. This can either be 1434 a string pointing to a module or an actual module, much like 1435 :setting:`ROOT_URLCONF`. 1436 1407 1437 .. setting:: TEST_RUNNER 1408 1438 1409 1439 TEST_RUNNER