Django

Code

Ticket #7521: 7521.diff

File 7521.diff, 6.0 kB (added by telenieko, 5 months ago)

Patch to allow "urls" parameter on tests.

  • a/django/contrib/formtools/tests.py

    old new  
    2121 
    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) 
    3029        input_template = '<input type="hidden" name="%s" value="%s" />' 
    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        """ 
    3834        Verifies name mangling to get uniue field name. 
  • a/django/core/urlresolvers.py

    old new  
    296296    kwargs = kwargs or {} 
    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() 
  • a/django/test/testcases.py

    old new  
    44 
    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) 
    1315 
     
    5456            * Flushing the database. 
    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        """ 
    5963        call_command('flush', verbosity=0, interactive=False) 
     
    6165            # We have to use this slightly awkward syntax due to the fact 
    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 
    6674    def __call__(self, result=None): 
     
    7987            result.addError(self, sys.exc_info()) 
    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, 
    84109                        target_status_code=200, host=None): 
  • a/docs/testing.txt

    old new  
    797797.. _dumpdata documentation: ../django-admin/#dumpdata-appname-appname 
    798798.. _loaddata documentation: ../django-admin/#loaddata-fixture-fixture 
    799799 
     800URLconf manipulation 
     801~~~~~~~~~~~~~~~~~~~~ 
     802 
     803If your application provides an urls.py file, there's no guarantee that the 
     804project running the tests will have all your urls included, which is likely 
     805to make your tests fail. 
     806 
     807For that, ``TestCase`` accepts a member attribute called ``urls``, the python 
     808module referenced (as a string) in this attribute will be used as the 
     809``ROOT_URLCONF`` during the run of this specific ``TestCase``, example:: 
     810 
     811    from django.test import TestCase 
     812     
     813    class TestMyViews(TestCase): 
     814        urls = 'myapp.urls' 
     815 
     816        def testIndexPageView(self): 
     817            # Here you'd test your view using ``Client``. 
     818 
     819 
    800820Emptying the test outbox 
    801821~~~~~~~~~~~~~~~~~~~~~~~~ 
    802822 
  • a/tests/regressiontests/test_client_regress/models.py

    old new  
    318318            self.client.get("/test_client_regress/staff_only/") 
    319319        except SuspiciousOperation: 
    320320            self.fail("Staff should be able to visit this page") 
     321 
     322# We need two different tests to check the urlconf subsitution, one to check 
     323# it was changed, anotherone (without self.urls) to check it was reverted on 
     324# teardown. Hence, the second must always be run after the first one. 
     325class UrlconfSubstitutionTests(TestCase): 
     326    urls = 'regressiontests.test_client_regress.urls' 
     327 
     328    def test_urlconf_was_changed(self): 
     329        url = reverse('arg_view', args=['somename']) 
     330        self.assertEquals(url, '/arg_view/somename/') 
     331 
     332class zUrlconfSubstitutionTests(TestCase): 
     333    def test_urlconf_was_reverted(self): 
     334        url = reverse('arg_view', args=['somename']) 
     335        self.assertEquals(url, '/test_client_regress/arg_view/somename/')