Ticket #7611: 7611.diff

File 7611.diff, 5.6 KB (added by Russell Keith-Magee, 8 years ago)

Slightly different approach to that originally described.

  • django/test/testcases.py

    11import re
     2import os
     3import sys
    24import unittest
    35from urlparse import urlsplit, urlunsplit
    5860              named fixtures.
    5961            * If the Test Case class has a 'urls' member, replace the
    6062              ROOT_URLCONF with it.
     63            * If the Test Case class has a 'template_dirs` member, replace the
     64              TEMPLATE_DIRS with the values in template_dirs interpreted as
     65              path names relative to the directory where the test is defined.
     66            * If the Test Case class has a 'template_loaders` member, replace the
     67              TEMPLATE_LOADERS with it.
    6168            * Clearing the mail test outbox.
    6269        """
    6370        call_command('flush', verbosity=0, interactive=False)
    6976            self._old_root_urlconf = settings.ROOT_URLCONF
    7077            settings.ROOT_URLCONF = self.urls
    7178            clear_url_caches()
     79        if hasattr(self, 'template_loaders'):
     80            self._old_template_loaders = settings.TEMPLATE_LOADERS
     81            settings.TEMPLATE_LOADERS = self.template_loaders
     82        if hasattr(self, 'template_dirs'):
     83            self._old_template_dirs = settings.TEMPLATE_DIRS
     84            test_dir = os.path.dirname(sys.modules[self.__module__].__file__)
     85            settings.TEMPLATE_DIRS = [ os.path.join(test_dir, dirname) for dirname in self.template_dirs ]
    7287        mail.outbox = []
    7489    def __call__(self, result=None):
    104119        if hasattr(self, '_old_root_urlconf'):
    105120            settings.ROOT_URLCONF = self._old_root_urlconf
    106121            clear_url_caches()
     122        if hasattr(self, '_old_template_loaders'):
     123            settings.TEMPLATE_LOADERS = self._old_template_loaders
     124        if hasattr(self, '_old_template_dirs'):
     125            settings.TEMPLATE_DIRS = self._old_template_dirs
    108127    def assertRedirects(self, response, expected_url, status_code=302,
    109128                        target_status_code=200, host=None):
  • django/contrib/auth/tests/basic.py

    6161class PasswordResetTest(TestCase):
    6262    fixtures = ['authtestdata.json']
    6363    urls = 'django.contrib.auth.urls'
     64    template_dirs = ('templates',)
     65    template_loaders = ('django.template.loaders.filesystem.load_template_source',)
    6567    def test_email_not_found(self):
    6668        "Error is raised if the provided email address isn't currently registered"
    6769        response = self.client.get('/password_reset/')
  • django/contrib/auth/tests/templates/registration/password_reset_form.html

     1{% if form.email.errors %}{{ form.email.errors }}{% endif %}
     2 No newline at end of file
  • django/contrib/auth/tests/templates/registration/password_reset_email.html

     1<p>A valid email address was provided for password reset</p>
     2 No newline at end of file
  • docs/testing.txt

    830830This test case will use the contents of ``myapp.test_urls`` as the
    831831URLconf for the duration of the test case.
     833Test templates
     836**New in Django development version**
     838If you are building a truly reusable application, you can't always rely
     839upon the fact that the project in which your application is deployed will
     840provide templates in the same way as templates being available.
     842In order to provide a reliable collection of templates for your tests,
     843``django.test.TestCase`` provides the ability to customize the template
     844directories and loaders for the duration of a test suite.
     846If your ``TestCase`` instance defines the attributes ``template_dirs``
     847or ``template_loaders``, the values provided will override the values for
     848``settings.TEMPLATE_DIRS`` and ``settings.TEMPLATE_LOADERS`` respectively,
     849for the duration of the tests.
     851The value of ``template_loaders`` is interpreted the same way that
     852``settings.TEMPLATE_LOADERS`` is interpreted in a normal settings file.
     853However, in order to avoid a dependency on a specific absolute project
     854directory, the directories named in ``template_dirs`` will be interpreted
     855as being relative to the directory containing the file in which the test
     856case is defined.
     858For example, consider the following test case::
     860    from django.test import TestCase
     862    class TestMyViews(TestCase):
     863        template_dirs = ('templates',)
     864        template_loaders = ('django.template.loaders.filesystem.load_template_source',)
     866        def testIndexPageView(self):
     867            # Here you'd test your view using ``Client``.
     869If this test case was defined in ``/foo/bar/tests/mytest.py``, then templates
     870would be loaded from ``/for/bar/tests/templates/`` for the duration of the
     871test case.
    833873Emptying the test outbox
Back to Top