繁体   English   中英

测试 Flask render_template() 上下文

[英]Test Flask render_template() context

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

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

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

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

您可以使用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必须提供您的烧瓶应用程序。

Flask 官方文档建议您使用template_rendered信号(自 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

我的建议是查看Flask 文档进行测试

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

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是您的应用程序/项目的名称。

您可能希望在 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 %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM