Django

Code

Changeset 7805

Show
Ignore:
Timestamp:
06/30/08 07:34:29 (2 months ago)
Author:
russellm
Message:

Fixed #7521 -- Added the ability to customize ROOT_URLCONF for the duration of a TestCase?. Thanks to Mark Fargas (telenieko) for his work on this patch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/formtools/tests.py

    r7705 r7805  
    2222 
    2323class PreviewTests(TestCase): 
     24    urls = 'django.contrib.formtools.test_urls' 
    2425 
    2526    def setUp(self): 
    26         self._old_root_urlconf = settings.ROOT_URLCONF 
    27         settings.ROOT_URLCONF = 'django.contrib.formtools.test_urls' 
    2827        # Create a FormPreview instance to share between tests 
    2928        self.preview = preview.FormPreview(TestForm) 
     
    3130        self.input = input_template % (self.preview.unused_name('stage'), "%d") 
    3231 
    33     def tearDown(self): 
    34         settings.ROOT_URLCONF = self._old_root_urlconf 
    35          
    3632    def test_unused_name(self): 
    3733        """ 
  • django/trunk/django/core/urlresolvers.py

    r7660 r7805  
    297297    return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname, *args, **kwargs)) 
    298298 
     299def clear_url_caches(): 
     300    global _resolver_cache 
     301    global _callable_cache 
     302    _resolver_cache.clear() 
     303    _callable_cache.clear() 
  • django/trunk/django/test/testcases.py

    r7578 r7805  
    55from django.http import QueryDict 
    66from django.db import transaction 
     7from django.conf import settings 
    78from django.core import mail 
    89from django.core.management import call_command 
    910from django.test import _doctest as doctest 
    1011from django.test.client import Client 
     12from django.core.urlresolvers import clear_url_caches 
    1113 
    1214normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) 
     
    5557            * If the Test Case class has a 'fixtures' member, installing the  
    5658              named fixtures. 
     59            * If the Test Case class has a 'urls' member, replace the 
     60              ROOT_URLCONF with it. 
    5761            * Clearing the mail test outbox. 
    5862        """ 
     
    6266            # that we're using *args and **kwargs together. 
    6367            call_command('loaddata', *self.fixtures, **{'verbosity': 0}) 
     68        if hasattr(self, 'urls'): 
     69            self._old_root_urlconf = settings.ROOT_URLCONF 
     70            settings.ROOT_URLCONF = self.urls 
     71            clear_url_caches() 
    6472        mail.outbox = [] 
    6573 
     
    8088            return 
    8189        super(TestCase, self).__call__(result) 
     90        try: 
     91            self._post_teardown() 
     92        except (KeyboardInterrupt, SystemExit): 
     93            raise 
     94        except Exception: 
     95            import sys 
     96            result.addError(self, sys.exc_info()) 
     97            return 
     98 
     99    def _post_teardown(self): 
     100        """ Performs any post-test things. This includes: 
     101 
     102            * Putting back the original ROOT_URLCONF if it was changed. 
     103        """ 
     104        if hasattr(self, '_old_root_urlconf'): 
     105            settings.ROOT_URLCONF = self._old_root_urlconf 
     106            clear_url_caches() 
    82107 
    83108    def assertRedirects(self, response, expected_url, status_code=302, 
  • django/trunk/docs/testing.txt

    r7578 r7805  
    798798.. _loaddata documentation: ../django-admin/#loaddata-fixture-fixture 
    799799 
     800URLconf configuration 
     801~~~~~~~~~~~~~~~~~~~~~ 
     802 
     803**New in Django development version** 
     804 
     805If your application provides views, you may want to include tests that 
     806use the test client to exercise those views. However, an end user is free 
     807to deploy the views in your application at any URL of their choosing. 
     808This means that your tests can't rely upon the fact that your views will 
     809be available at a particular URL. 
     810 
     811In order to provide a reliable URL space for your test, 
     812``django.test.TestCase`` provides the ability to customize the URLconf 
     813configuration for the duration of the execution of a test suite. 
     814If your ``TestCase`` instance defines an ``urls`` attribute, the 
     815``TestCase`` will use the value of that attribute as the ``ROOT_URLCONF`` 
     816for the duration of that test.  
     817 
     818For example:: 
     819 
     820    from django.test import TestCase 
     821     
     822    class TestMyViews(TestCase): 
     823        urls = 'myapp.test_urls' 
     824 
     825        def testIndexPageView(self): 
     826            # Here you'd test your view using ``Client``. 
     827 
     828This test case will use the contents of ``myapp.test_urls`` as the 
     829URLconf for the duration of the test case. 
     830 
    800831Emptying the test outbox 
    801832~~~~~~~~~~~~~~~~~~~~~~~~ 
  • django/trunk/tests/regressiontests/test_client_regress/models.py

    r7583 r7805  
    319319        except SuspiciousOperation: 
    320320            self.fail("Staff should be able to visit this page") 
     321 
     322# We need two different tests to check URLconf subsitution -  one to check 
     323# it was changed, and another one (without self.urls) to check it was reverted on 
     324# teardown. This pair of tests relies upon the alphabetical ordering of test execution. 
     325class UrlconfSubstitutionTests(TestCase): 
     326    urls = 'regressiontests.test_client_regress.urls' 
     327 
     328    def test_urlconf_was_changed(self): 
     329        "TestCase can enforce a custom URLConf on a per-test basis" 
     330        url = reverse('arg_view', args=['somename']) 
     331        self.assertEquals(url, '/arg_view/somename/') 
     332 
     333# This test needs to run *after* UrlconfSubstitutionTests; the zz prefix in the 
     334# name is to ensure alphabetical ordering. 
     335class zzUrlconfSubstitutionTests(TestCase): 
     336    def test_urlconf_was_reverted(self): 
     337        "URLconf is reverted to original value after modification in a TestCase" 
     338        url = reverse('arg_view', args=['somename']) 
     339        self.assertEquals(url, '/test_client_regress/arg_view/somename/')