Question

I am writing some tests for a Django app (one that also makes use of Django-CMS). The app itself doesn't use any templates, but a scenario in the tests involves creating a Django-CMS page, thus a template. What's the easiest and quickest way to define and use a template required just for testing?

Was it helpful?

Solution

Looks like defining a custom template loader did the trick for me:

from django.template.loader import BaseLoader
from django.template.base import TemplateDoesNotExist


class TestTemplateLoader(BaseLoader):

    def __init__(self, *args, **kwargs):
        super(TestTemplateLoader, self).__init__(*args, **kwargs)
        self.is_usable = True

    def load_template_source(self, template_name, template_dirs=None):
        if template_name == 'test.html':
            return 'test.html', 'test.html'
        raise TemplateDoesNotExist

Then included it in the test settings file:

CMS_TEMPLATES = (
    ('test.html', 'test.html'),
)

TEMPLATE_LOADERS = (
    'cmsplugin_filer_image.tests.utils.TestTemplateLoader',
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top