diff --git a/django/contrib/formtools/tests.py b/django/contrib/formtools/tests.py
index 143857a..ba241e9 100644
a
|
b
|
class TestForm(forms.Form):
|
21 | 21 | |
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_URLCONF |
27 | | 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) |
30 | 29 | input_template = '<input type="hidden" name="%s" value="%s" />' |
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_urlconf |
35 | | |
36 | 32 | def test_unused_name(self): |
37 | 33 | """ |
38 | 34 | Verifies name mangling to get uniue field name. |
diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
index 2ad63bf..ff0bcbc 100644
a
|
b
|
def reverse(viewname, urlconf=None, args=None, kwargs=None):
|
296 | 296 | kwargs = kwargs or {} |
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() |
diff --git a/django/test/testcases.py b/django/test/testcases.py
index ee83b96..3bad399 100644
a
|
b
|
from urlparse import urlsplit, urlunsplit
|
4 | 4 | |
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) |
13 | 15 | |
… |
… |
class TestCase(unittest.TestCase):
|
54 | 56 | * Flushing the database. |
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 | """ |
59 | 63 | call_command('flush', verbosity=0, interactive=False) |
… |
… |
class TestCase(unittest.TestCase):
|
61 | 65 | # We have to use this slightly awkward syntax due to the fact |
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 | |
66 | 74 | def __call__(self, result=None): |
… |
… |
class TestCase(unittest.TestCase):
|
79 | 87 | result.addError(self, sys.exc_info()) |
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, |
84 | 109 | target_status_code=200, host=None): |
diff --git a/docs/testing.txt b/docs/testing.txt
index befa697..67d2cfc 100644
a
|
b
|
another test, or by the order of test execution.
|
797 | 797 | .. _dumpdata documentation: ../django-admin/#dumpdata-appname-appname |
798 | 798 | .. _loaddata documentation: ../django-admin/#loaddata-fixture-fixture |
799 | 799 | |
| 800 | URLconf manipulation |
| 801 | ~~~~~~~~~~~~~~~~~~~~ |
| 802 | |
| 803 | If your application provides an urls.py file, there's no guarantee that the |
| 804 | project running the tests will have all your urls included, which is likely |
| 805 | to make your tests fail. |
| 806 | |
| 807 | For that, ``TestCase`` accepts a member attribute called ``urls``, the python |
| 808 | module 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 | |
800 | 820 | Emptying the test outbox |
801 | 821 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
802 | 822 | |
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
index a204ec3..48919e0 100644
a
|
b
|
class ExceptionTests(TestCase):
|
318 | 318 | self.client.get("/test_client_regress/staff_only/") |
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 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. |
| 325 | class 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 | |
| 332 | class 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/') |