【问题标题】:Django testing for templates loaded via template tagsDjango 测试通过模板标签加载的模板
【发布时间】:2013-04-22 00:48:03
【问题描述】:

我正在为一个应用编写一套测试。

在很多地方我都使用 Django 的方便的“assertTemplateUsed”。这适用于所有模板,据我所知,通过模板标签(作为包含标签对象)加载的模板除外。

当通过模板标签加载模板时,是否有另一种方法可以使用断言模板?我当然可以检查模板中的字符串,但是检查模板的使用会是一个更好的解决方案。

【问题讨论】:

    标签: django django-templates django-testing


    【解决方案1】:

    尝试捕获template_rendered signal,它在测试期间可用。

    来自文档:

    django.test.signals.template_rendered
    
    Sent when the test system renders a template. This signal is not emitted during normal operation of a Django server – it is only available during testing.
    
    Arguments sent with this signal:
    
    sender
        The Template object which was rendered.
    template
        Same as sender
    context
        The Context with which the template was rendered. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-28
      • 2012-01-26
      • 2010-11-15
      • 1970-01-01
      • 2010-11-29
      • 2014-05-11
      • 2017-07-29
      • 2018-10-21
      相关资源
      最近更新 更多