【问题标题】:Test Flask render_template() context测试 Flask render_template() 上下文
【发布时间】:2014-07-22 03:49:44
【问题描述】:

我有一个如下所示的 Flask 路由:

@app.route('/')                                                                 
def home():                                                                                                                  
    return render_template(                                                     
        'home.html',                                                            
        greeting:"hello"                                       
    )                                                                           

如何测试'home.html' 模板是否已呈现,以及render_template() 上下文是否使用特定值定义了greeting 变量?

这些应该(并且可能)很容易测试,但我真的不确定如何使用 Flask 和 unittest 来测试。

【问题讨论】:

  • 你终于找到解决办法了吗?
  • 我猜你想写greeting:"hello"而不是greeting="hello"

标签: python flask jinja2


【解决方案1】:

可以使用flask-testing提供的TestCaseassert_template_used方法。

from flask.ext.testing import TestCase

class MyTest(TestCase):

    def create_app(self):
        return myflaskapp

    def test_greeting(self):
        self.app.get('/')
        self.assert_template_used('hello.html')
        self.assert_context("greeting", "hello")

create_app 方法必须提供您的烧瓶应用程序。

【讨论】:

【解决方案2】:

Flask official documentation 建议您使用template_rendered signal (自 0.6 版起可用) 对您的模板和用于渲染它的变量进行单元测试。

例如,这里有一个辅助上下文管理器,可以在单元测试中用于确定渲染了哪些模板以及将哪些变量传递给了模板:

from flask import template_rendered
from contextlib import contextmanager

@contextmanager
def captured_templates(app):
    recorded = []
    def record(sender, template, context, **extra):
        recorded.append((template, context))
    template_rendered.connect(record, app)
    try:
        yield recorded
    finally:
        template_rendered.disconnect(record, app)

现在可以轻松地与测试客户端配对:

with captured_templates(app) as templates:
    rv = app.test_client().get('/')
    assert rv.status_code == 200
    assert len(templates) == 1
    template, context = templates[0]
    assert template.name == 'index.html'
    assert len(context['items']) == 10

【讨论】:

  • 信号所基于的 blinker 库自 2015 年以来没有任何主要版本。
【解决方案3】:

我的建议是看看Flask documentation for testing

使用文档作为指导,您应该能够设置一个可以检查响应内容的测试用例。

import unittest
import yourappname

class MyAppTestCase(unittest.TestCase):
    def setUp(self):
        self.app = yourappname.app.test_client()

    def test_greeting(self):
        rv = self.app.get('/')
        self.assertIn('hello', rv.data)

yourappname 是您的应用/项目的名称。

【讨论】:

  • 谢谢,斯蒂芬穆斯。我查看了 Flask 文档以进行测试,但结果是空的。您的解决方案测试响应。我要测试的是request,具体来说,是传递给render_template() 的值。我可以使用test_request_context(),然后访问flask.request,但这并没有让我更接近于测试我想要的请求。
【解决方案4】:

您可能希望在您的 Html 页面中使用 Jinja 设置,将变量传递到页面并查看是否更新。

http://flask.pocoo.org/docs/0.11/templating/#jinja-setup

http://jinja.pocoo.org/

举个例子:

烧瓶模板

@app.route('/')                                                                 
def home():                                                                                                                  
    return render_template(                                                     
        'home.html',                                                            
        greetingDictionary = {"greeting": "hello" , "forthoseabouttorock" :"wesaluteyou" }                                       
    )   

html页面

{% for key in greetingDictionary %}
<h1>{{key}}</h1>
<p>{{greetingDictionary[key]}}</p>
{% endfor %}

【讨论】:

  • 他的意思是通过程序进行测试。
  • 我在谷歌搜索“渲染模板上下文示例烧瓶”,这是第一个出现的帖子。这条评论让我更好地了解我想要理解的内容。
  • @CorinaRoca 我不明白这个答案是否有用,但如果最后这里的一些答案对你有帮助的话,那就太好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 2016-06-10
相关资源
最近更新 更多