Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Flask render_template() context

I have a Flask route that looks like this:

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

How do I test that the 'home.html' template was rendered, and that the render_template() context defined the greeting variable with a particular value?

These should be (and probably are) pretty easy to test, but I'm really not sure how to do this with Flask and unittest.

like image 589
Shailen Tuli Avatar asked Jun 02 '14 04:06

Shailen Tuli


People also ask

What does Render_template do in Flask?

render_template is a Flask function from the flask. templating package. render_template is used to generate output from a template file based on the Jinja2 engine that is found in the application's templates folder. Note that render_template is typically imported directly from the flask package instead of from flask.

What is the difference between Render_template and redirect?

redirect returns a 302 header to the browser, with its Location header as the URL for the index function. render_template returns a 200, with the index. html template returned as the content at that URL.

How do I use a render template in Flask?

In this code block, you import the Flask class and the render_template() function from the flask package. You use the Flask class to create your Flask application instance named app . Then you define a view function (which is a Python function that returns an HTTP response) called hello() using the app.


2 Answers

You can use the assert_template_used method of TestCase provided by flask-testing.

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") 

The method create_app must provide your flask app.

like image 115
Jarus Avatar answered Sep 28 '22 21:09

Jarus


Flask official documentation suggests that you use the template_rendered signal (available since version 0.6) for unit-testing your templates and the variables used to render it.

For example, here is a helper context manager that can be used in a unittest to determine which templates were rendered and what variables were passed to the template:

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) 

This can now easily be paired with a test client:

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 
like image 45
tilacog Avatar answered Sep 28 '22 22:09

tilacog