Skip to content
Permalink
Browse files

Introducing the 'render_templates' option

Now the developer can choose to not render the real templates when
running the test cases. This can be useful to isolate the tests from the
template.

Inspired in the RSpec Rails 'render_views':
https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/controller-specs/render-views
  • Loading branch information
poiati committed Sep 3, 2012
1 parent dac78d1 commit 382c9565f8c460d876d23ee0b8562e433d057240
Showing with 64 additions and 1 deletion.
  1. +23 −1 flask_testing/utils.py
  2. +41 −0 tests/test_utils.py
@@ -15,7 +15,7 @@
from werkzeug import cached_property

# Use Flask's preferred JSON module so that our runtime behavior matches.
from flask import json_available
from flask import json_available, templating
if json_available:
from flask import json

@@ -50,6 +50,16 @@ class TestResponse(response_class, JsonResponseMixin):

return TestResponse

def _empty_render(template, context, app):
"""
Used to monkey patch the render_template flask method when
the render_templates property is set to False in the TestCase
"""
if _is_signals:
from flaks import template_rendered
template_rendered.send(app, template=template, context=context)

return ""

class TestCase(unittest.TestCase):

@@ -85,6 +95,9 @@ def _pre_setup(self):
self._ctx = self.app.test_request_context()
self._ctx.push()

if self._is_not_render_templates():
self._monkey_patch_render_template()

self.templates = []
if _is_signals:
template_rendered.connect(self._add_template)
@@ -99,6 +112,15 @@ def _post_teardown(self):
self.app.response_class = self._orig_response_class
if _is_signals:
template_rendered.disconnect(self._add_template)
if hasattr(self, '_true_render'):
templating._render = self._true_render

def _is_not_render_templates(self):
return hasattr(self, 'render_templates') and not self.render_templates

def _monkey_patch_render_template(self):
self._true_render = templating._render
templating._render = _empty_render

def assertTemplateUsed(self, name):
"""
@@ -1,3 +1,4 @@
from unittest import TestResult
from flask_testing import TestCase
from flask_testing.utils import ContextVariableDoesNotExist
from flask_app import create_app
@@ -89,3 +90,43 @@ def test_assert_get_context_variable_not_exists(self):
self.get_context_variable, "foo")
except RuntimeError:
pass

class TestNotRenderTemplates(TestCase):

render_templates = False

def create_app(self):
return create_app()

def test_assert_not_process_the_template(self):
response = self.client.get("/template/")

assert "" == response.data

class TestRenderTemplates(TestCase):

render_templates = True

def create_app(self):
return create_app()

def test_assert_not_process_the_template(self):
response = self.client.get("/template/")

assert "" != response.data

class TestRestoreTheRealRender(TestCase):

def create_app(self):
return create_app()

def test_assert_the_real_render_template_is_restored(self):
test = TestNotRenderTemplates('test_assert_not_process_the_template')
test_result = TestResult()
test(test_result)

assert test_result.wasSuccessful()

response = self.client.get("/template/")

assert "" != response.data

0 comments on commit 382c956

Please sign in to comment.
You can’t perform that action at this time.