This is a short passage of one of my test files, where self.render_template a simple helper method in the TestCase is:
rendered = self.render_template(
'{% load templatequery %}'
'{% displayquery django_templatequery.KeyValue all() with "list.html" %}'
)
self.assertEqual(rendered,"foo=0
bar=50
spam=100
egg=200
")
self.assertRaises(
template.TemplateSyntaxError,
self.render_template,
'{% load templatequery %}'
'{% displayquery django_templatequery.KeyValue all() notwith "list.html" %}'
)
It is very basic and uses blackbox testing. It just takes a string as template source, renders it and checks if the output equals the expected string.
The render_template
method is quite simplistic:
from django.template import Context, Template
class MyTest(TestCase):
def render_template(self, string, context=None):
context = context or {}
context = Context(context)
return Template(string).render(context)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…