测试自定义Django模板过滤器 [英] Testing a custom Django template filter
本文介绍了测试自定义Django模板过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在project/app/templatetags
下创建了一个自定义模板过滤器.
I have a custom template filter I created under project/app/templatetags
.
我想为刚刚发现的一些错误添加一些回归测试.我将如何去做?
I want to add some regression tests for some bugs I just found. How would I go about doing so?
推荐答案
Here's how I do it (extracted from my django-multiforloop):
from django.test import TestCase
from django.template import Context, Template
class TagTests(TestCase):
def tag_test(self, template, context, output):
t = Template('{% load multifor %}'+template)
c = Context(context)
self.assertEqual(t.render(c), output)
def test_for_tag_multi(self):
template = "{% for x in x_list; y in y_list %}{{ x }}:{{ y }}/{% endfor %}"
context = {"x_list": ('one', 1, 'carrot'), "y_list": ('two', 2, 'orange')}
output = u"one:two/1:2/carrot:orange/"
self.tag_test(template, context, output)
This is fairly similar to how tests are laid out in Django's own test suite, but without relying on django's somewhat complicated testing machinery.
这篇关于测试自定义Django模板过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文