Flask: Testing render_template()

Created on 8 Jun 2014  路  5Comments  路  Source: pallets/flask

How do I test this line?
return render_template(u'http_error.html', error_detail=detail)

I am a beginner as far as flask testing is concerned.

Could you document the answer to this on your testing page? http://flask.pocoo.org/docs/testing/

There is an unanswered question similar to this on stackoverflow.
http://stackoverflow.com/questions/23987564/test-flask-render-template-context

Most helpful comment

You shouldn't test that render_template() works because you shouldn't test how your application is implemented.

Tests exist to precisely and formally define what your application does and they do so by making sure that _using_ the application produces the results you expect it to.

The fact that a web application even uses a database or templates is an implementation detail and completely irrelevant for the tests.

For that reason the documentation does not contain any information on how to do such a thing nor will it be included.

All 5 comments

Please do not use the bug tracker as support forum :) Not sure what there is to test about template rendering. There are examples for testing general responses on the testing page you linked.

I'm requesting that your documentation contain a feature that will help me and others test their code. It says on your site, "Something that is untested is broken."

The Issue I am raising is that a heavily used method render_template() does not appear to be fully testable in a direct sort of manner, and could use more documentation.

It is a little strange that I need to use get() to test render_template(). This leads to implicit rather than explicit testing of render_template(). If I use get() and discover that something isn't showing up, then I'm probably not using render_template() correctly or my template doesn't display the data correctly or I may have a database error.

I can write tests that check the database call and eliminate it as the source of the error. Why can't I do the same with render_template(), and narrow it down to the template itself?

I'm assuming that this is possible, but undocumented. Thus my request that it be documented.

This raises another question: Can I test whether the render_template is providing the template with the correct parameters? e.g. The template expects a variable 'dog', but render template provides 'octopus'. Is there an automated way to test this?

Why would you write unit tests in your application to test core framework functionality?

Anyway, maybe the template_rendered signal is helpful for you.

You shouldn't test that render_template() works because you shouldn't test how your application is implemented.

Tests exist to precisely and formally define what your application does and they do so by making sure that _using_ the application produces the results you expect it to.

The fact that a web application even uses a database or templates is an implementation detail and completely irrelevant for the tests.

For that reason the documentation does not contain any information on how to do such a thing nor will it be included.

@DasIch Didn't quite get your comment first read through (my misunderstanding of testing), but cheers, great explanation.

Was this page helpful?
0 / 5 - 0 ratings