【问题标题】:How to test custom template tags in Django?如何在 Django 中测试自定义模板标签?
【发布时间】:2010-12-13 20:34:23
【问题描述】:

我正在向 Django 应用程序添加一组模板标签,但我不确定如何测试它们。我已经在我的模板中使用了它们,它们似乎正在工作,但我正在寻找更正式的东西。主要逻辑在模型/模型管理器中完成并且已经过测试。标签只是检索数据并将其存储在上下文变量中,例如

{% views_for_object widget as views %}
"""
Retrieves the number of views and stores them in a context variable.
"""
# or
{% most_viewed_for_model main.model_name as viewed_models %}
"""
Retrieves the ViewTrackers for the most viewed instances of the given model.
"""

所以我的问题是你通常会测试你的模板标签吗?如果你这样做了,你会怎么做?

【问题讨论】:

    标签: django django-templates django-testing


    【解决方案1】:

    字符串可以呈现为模板,因此您可以编写一个包含简单“模板”的测试,使用您的模板标签作为字符串,并确保它在给定特定上下文的情况下正确呈现。

    【讨论】:

    • 这些标签只呈现一个空字符串,而是改变上下文,但我应该也可以测试。
    【解决方案2】:

    当我测试我的模板标签时,我会让标签本身返回一个包含我正在使用的文本或字典的字符串......有点像其他建议的行。

    由于标签可以修改上下文和/或返回要呈现的字符串——我发现仅查看呈现的字符串是最快的。

    代替:

    return ''
    

    拥有它:

    return str(my_data_that_I_am_testing)
    

    直到你开心为止。

    【讨论】:

      【解决方案3】:

      你们让我走上了正轨。可以检查渲染后上下文是否正确更改:

      class TemplateTagsTestCase(unittest.TestCase):        
          def setUp(self):    
              self.obj = TestObject.objects.create(title='Obj a')
      
          def testViewsForOjbect(self):
              ViewTracker.add_view_for(self.obj)
              t = Template('{% load my_tags %}{% views_for_object obj as views %}')
              c = Context({"obj": self.obj})
              t.render(c)
              self.assertEqual(c['views'], 1)
      

      【讨论】:

        【解决方案4】:

        这是我的一个测试文件的一小段,其中 self.render_template 是 TestCase 中一个简单的辅助方法:

            rendered = self.render_template(
                '{% load templatequery %}'
                '{% displayquery django_templatequery.KeyValue all() with "list.html" %}'
            )
            self.assertEqual(rendered,"foo=0\nbar=50\nspam=100\negg=200\n")
        
            self.assertRaises(
                template.TemplateSyntaxError,
                self.render_template,
                '{% load templatequery %}'
                '{% displayquery django_templatequery.KeyValue all() notwith "list.html" %}'
            )
        

        它非常基础,使用黑盒测试。它只是将一个字符串作为模板源,渲染它并检查输出是否等于预期的字符串。

        render_template 方法相当简单:

        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)
        

        【讨论】:

        • 您如何在项目范围之外进行测试,例如,对于可重用应用程序?渲染包含{% load custom_tag %} 的模板字符串似乎不起作用,至少不需要额外的工作?
        • 回答了我自己的问题:使用register = template.Library(); template.libraries['django.templatetags.mytest'] = register; register.tag(name='custom_tag', compile_function=custom_tag)
        • 您的代码示例中的self 是什么? My TestCase 对象没有render_template 方法
        • @Gregor Müllegger 您能否更新您的答案并包含render_template 方法?谢谢
        • @yamm:我刚刚添加了一个包含render_template 方法的sn-p。希望这会有所帮助。
        【解决方案5】:

        如何测试模板标签test of flatpage templatetags的一个很好的例子

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-20
          • 1970-01-01
          • 2021-07-13
          • 2012-03-15
          • 2019-12-01
          • 2018-06-17
          • 1970-01-01
          相关资源
          最近更新 更多