This is a small addition to the mako template processing plugin for django that allows you to use the unit test framework with mako templates. To install, put the code into a file on your python path, and add the python path to your settings.py file. For example, if you install the code at
/usr/lib/python2.5/site-packages/mako_django/test_integration.py
you would add the following line to settings.py:
TEST_RUNNER="mako_django.test_integration.run_mako_tests"
This code will still call all of the normal test code, it just adds the mako template handler onto the list of things that are monitored.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | # mako_django unit test integration. To Install,
# put this file somewhere in your python path,
# and put the string
# TEST_RUNNER=<python path to this file>
# in your settings.py file in your site. For example,
# if you install it at site-packages/mako_django/test_integration.py,
# this would go in settings.py:
# TEST_RUNNER="mako_django.test_integration.run_mako_tests"
#**** This is ALPHA code (at best). ******
# Written by Erik Lee
# You have my permission to do anything you want with it, except that
# you may not assume it is correct!
from django.test.utils import setup_test_environment, teardown_test_environment
from django.test.simple import run_tests
from mako.template import Template
from django.template.context import Context
from django.test import signals
def instrumented_test_render(self,**context):
"""
This function calls the template's render function with the given
rendering context, and then sends the template_rendered signal
with the context and the template being rendered in order to
integrate more fully with django's unit test system.
"""
self.pretest_render(**context)
signals.template_rendered.send(sender=self, template=self, context=Context(context))
def setup():
"""
This function simply hijacks the mako template renderer's render
function with a simple wrapper (instrumented_test_render) taht
will trigger the template_rendered signal after rendering the
template. After the tests are run (using the default test runner
provided in the django distribution, it restores the rendering
function to its original state. During the tests, the original
function can be accessed through the name "pretest_render"
"""
Template.pretest_render = Template.render
Template.render = instrumented_test_render
def finish():
"""
Restore the template renderer function to its original value
"""
Template.render = Template.pretest_render
def run_mako_tests(test_labels, verbosity, interactive):
"""
Emulate the behavior of the django.test.simple.run_tests function
"""
setup()
result = run_tests(test_labels, verbosity, interactive)
finish()
return result
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.