notreadbyhumans
notreadbyhumans

Reputation: 617

Unit testing Django templates in Google App Engine raises TemplateDoesNotExist(name)

I am trying to set up unit testing of a series of Django templates in Google App Engine using the python unittest framework.

The app uses python 2.7, webapp2, Django 1.2, and is already using unittest for testing the none Django functionality. The Django templates have no issues serving through the server in dev and live environments.

When executing the page call through the unittest framework the following error is raised:

  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_1_2/django/template/loader.py", line 138, in find_template
  raise TemplateDoesNotExist(name)
  TemplateDoesNotExist: site/index.html

Settings.py:

import os

PROJECT_ROOT = os.path.dirname(__file__)

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, "templates"),
)

Unit test call:

import webapp2
import unittest
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

import main

class test_main(unittest.TestCase):

    def test_default_page(self):

        request = webapp2.Request.blank('/')

        response = request.get_response(main.app)

        self.assertEqual(response.status_int, 200)

I've found it necessary to specify DJANGO_SETTINGS_MODULE and the Django version within the test file even though these are normally specified through app.yaml

The page handler:

from django.template.loaders.filesystem import Loader
from django.template.loader import render_to_string

class default(webapp2.RequestHandler):

    def get(self):

        self.response.out.write(render_to_string('site/index.html'))

I've tried stripping everything out of index.html but still get the same error.

I've tried using an explicit path for TEMPLATE_DIRS but no change.

Any ideas?

Upvotes: 0

Views: 602

Answers (1)

dragonx
dragonx

Reputation: 15143

Check if this is set anywhere in your django config:

TEMPLATE_LOADERS=('django.template.loaders.filesystem.load_template_source',)

Upvotes: 1

Related Questions