Python Test Flask render_template()上下文

Python Test Flask render_template()上下文,python,flask,jinja2,Python,Flask,Jinja2,我有一个烧瓶路线,看起来像这样: @app.route('/') def home(): return render_template

我有一个烧瓶路线,看起来像这样:

@app.route('/')                                                                 
def home():                                                                                                                  
    return render_template(                                                     
        'home.html',                                                            
        greeting:"hello"                                       
    )                                                                           
如何测试
'home.html'
模板是否已呈现,以及
呈现模板()
上下文是否使用特定值定义了
问候语
变量


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

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

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

您可以使用所使用的
TestCase
方法的
assert\u模板

方法
create\u app
必须提供您的flask app。

建议您使用(自版本0.6起提供)对模板和用于呈现模板的变量进行单元测试

例如,以下是一个帮助器上下文管理器,可以在unittest中使用它来确定呈现了哪些模板以及传递给模板的变量:

来自flask导入模板\u
从contextlib导入contextmanager
@上下文管理器
def捕获的_模板(应用程序):
记录=[]
def记录(发件人、模板、上下文,**额外):
记录。追加((模板,上下文))
模板\u呈现。连接(记录、应用)
尝试:
产量记录
最后:
已呈现模板。断开连接(记录、应用)
现在可以轻松地与测试客户端配对:

将捕获的_模板(应用程序)作为模板:
rv=app.test_client().get(“/”)
断言rv.status_代码==200
断言len(模板)==1
模板,上下文=模板[0]
assert template.name==“index.html”
断言len(上下文['items'])==10

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

例如:

烧瓶模板 html页面
{%用于greetingDictionary%中的键]
{{key}}
{{greetingDictionary[key]}

{%endfor%}
谢谢,斯蒂芬穆斯。我已经看了烧瓶文档进行测试,结果是空的。您的解决方案测试响应。我想测试的是请求,特别是传递给
render\u template()
的值。我可以使用
test\u request\u context()
,然后访问
flask.request
,但这并不能让我更接近我想要的测试请求。他的意思是以编程方式进行测试。我在google“render template context examples flask”中搜索,这是第一篇出现的帖子。这条评论让我对我试图理解的内容有了更好的了解。@CorinaRoca我不明白这个答案是否有用,如果最后这里的一些答案对你有帮助,那就好了。行了!只是必须这样导入:
从flask_testing导入TestCase
您最终得到了解决方案吗?我想不是
问候语:“hello”
您想写
问候语=“hello”
?信号所基于的闪烁器库自2015年以来没有任何主要版本。
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")
@app.route('/')                                                                 
def home():                                                                                                                  
    return render_template(                                                     
        'home.html',                                                            
        greetingDictionary = {"greeting": "hello" , "forthoseabouttorock" :"wesaluteyou" }                                       
    )   
{% for key in greetingDictionary %}
<h1>{{key}}</h1>
<p>{{greetingDictionary[key]}}</p>
{% endfor %}