Changeset 7805
- Timestamp:
- 06/30/08 07:34:29 (2 months ago)
- Files:
-
- django/trunk/django/contrib/formtools/tests.py (modified) (2 diffs)
- django/trunk/django/core/urlresolvers.py (modified) (1 diff)
- django/trunk/django/test/testcases.py (modified) (4 diffs)
- django/trunk/docs/testing.txt (modified) (1 diff)
- django/trunk/tests/regressiontests/test_client_regress/models.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/formtools/tests.py
r7705 r7805 22 22 23 23 class PreviewTests(TestCase): 24 urls = 'django.contrib.formtools.test_urls' 24 25 25 26 def setUp(self): 26 self._old_root_urlconf = settings.ROOT_URLCONF27 settings.ROOT_URLCONF = 'django.contrib.formtools.test_urls'28 27 # Create a FormPreview instance to share between tests 29 28 self.preview = preview.FormPreview(TestForm) … … 31 30 self.input = input_template % (self.preview.unused_name('stage'), "%d") 32 31 33 def tearDown(self):34 settings.ROOT_URLCONF = self._old_root_urlconf35 36 32 def test_unused_name(self): 37 33 """ django/trunk/django/core/urlresolvers.py
r7660 r7805 297 297 return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname, *args, **kwargs)) 298 298 299 def 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 5 5 from django.http import QueryDict 6 6 from django.db import transaction 7 from django.conf import settings 7 8 from django.core import mail 8 9 from django.core.management import call_command 9 10 from django.test import _doctest as doctest 10 11 from django.test.client import Client 12 from django.core.urlresolvers import clear_url_caches 11 13 12 14 normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s) … … 55 57 * If the Test Case class has a 'fixtures' member, installing the 56 58 named fixtures. 59 * If the Test Case class has a 'urls' member, replace the 60 ROOT_URLCONF with it. 57 61 * Clearing the mail test outbox. 58 62 """ … … 62 66 # that we're using *args and **kwargs together. 63 67 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() 64 72 mail.outbox = [] 65 73 … … 80 88 return 81 89 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() 82 107 83 108 def assertRedirects(self, response, expected_url, status_code=302, django/trunk/docs/testing.txt
r7578 r7805 798 798 .. _loaddata documentation: ../django-admin/#loaddata-fixture-fixture 799 799 800 URLconf configuration 801 ~~~~~~~~~~~~~~~~~~~~~ 802 803 **New in Django development version** 804 805 If your application provides views, you may want to include tests that 806 use the test client to exercise those views. However, an end user is free 807 to deploy the views in your application at any URL of their choosing. 808 This means that your tests can't rely upon the fact that your views will 809 be available at a particular URL. 810 811 In order to provide a reliable URL space for your test, 812 ``django.test.TestCase`` provides the ability to customize the URLconf 813 configuration for the duration of the execution of a test suite. 814 If your ``TestCase`` instance defines an ``urls`` attribute, the 815 ``TestCase`` will use the value of that attribute as the ``ROOT_URLCONF`` 816 for the duration of that test. 817 818 For 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 828 This test case will use the contents of ``myapp.test_urls`` as the 829 URLconf for the duration of the test case. 830 800 831 Emptying the test outbox 801 832 ~~~~~~~~~~~~~~~~~~~~~~~~ django/trunk/tests/regressiontests/test_client_regress/models.py
r7583 r7805 319 319 except SuspiciousOperation: 320 320 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. 325 class 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. 335 class 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/')
