librelist archives

« back to archive

Opt to not render template during tests

Opt to not render template during tests

From:
Paulo Gabriel Poiati
Date:
2012-08-18 @ 02:37
Hello all,

There is a way to not render the real template of the view in the
render_template method? I mean something like *render_views* in the rspec:

https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/controller-specs/render-views(by
default rails not render the views).

Thanks in advance!

[]'s
Paulo Poiati

blog.paulopoiati.com

Re: [flask] Opt to not render template during tests

From:
Steven Kryskalla
Date:
2012-08-18 @ 07:14
On Fri, Aug 17, 2012 at 7:37 PM, Paulo Gabriel Poiati
<paulogpoiati@gmail.com> wrote:
> Hello all,
>
> There is a way to not render the real template of the view in the
> render_template method? I mean something like render_views in the rspec:
> 
https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/controller-specs/render-views
> (by default rails not render the views).
>

You could mock out or monkeypatch flask's template rendering functions
with a new function that just returns an empty string, e.g.:

https://gist.github.com/3385009

-steve

Re: [flask] Opt to not render template during tests

From:
Adrian Ratnapala
Date:
2012-08-19 @ 12:11
One thing I do is use a wrapper for render_template that remembers
what the page and the context are.  The view is rendered as usual.  I
can then inspect that record during tests.  I am not sure if this is a
solution to your solution, and it is a bit hackish, but it works for
me.

On 18 August 2012 04:37, Paulo Gabriel Poiati <paulogpoiati@gmail.com> wrote:
> Hello all,
>
> There is a way to not render the real template of the view in the
> render_template method? I mean something like render_views in the rspec:
> 
https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/controller-specs/render-views
> (by default rails not render the views).
>
> Thanks in advance!
>
> []'s
> Paulo Poiati
>
> blog.paulopoiati.com



-- 
Adrian Ratnapala
mobile: +49 1515 277 0000

NGBitS GmbH
Rainer Strasse 7
83104 Ostermünchen

Web: www.ngbits.com

Re: [flask] Opt to not render template during tests

From:
Paulo Gabriel Poiati
Date:
2012-08-19 @ 17:54
Steven your solution is great but I have one problem with that when I mock
the method 'flask.templating._render' the flask-testing extension do not
record the template rendered through the signal.

Adrian your solutions is clever, creating a wrapper to render the templates
will resolve my problem at the cost of one more level of abstraction just
for the sake of testing what in my point of views is acceptable.

[]'s
Paulo Poiati

blog.paulopoiati.com


On Sun, Aug 19, 2012 at 9:11 AM, Adrian Ratnapala <
adrian.ratnapala@gmail.com> wrote:

> One thing I do is use a wrapper for render_template that remembers
> what the page and the context are.  The view is rendered as usual.  I
> can then inspect that record during tests.  I am not sure if this is a
> solution to your solution, and it is a bit hackish, but it works for
> me.
>
> On 18 August 2012 04:37, Paulo Gabriel Poiati <paulogpoiati@gmail.com>
> wrote:
> > Hello all,
> >
> > There is a way to not render the real template of the view in the
> > render_template method? I mean something like render_views in the rspec:
> >
> 
https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/controller-specs/render-views
> > (by default rails not render the views).
> >
> > Thanks in advance!
> >
> > []'s
> > Paulo Poiati
> >
> > blog.paulopoiati.com
>
>
>
> --
> Adrian Ratnapala
> mobile: +49 1515 277 0000
>
> NGBitS GmbH
> Rainer Strasse 7
> 83104 Ostermünchen
>
> Web: www.ngbits.com
>

Re: [flask] Opt to not render template during tests

From:
Paulo Gabriel Poiati
Date:
2012-08-19 @ 17:57
Sorry, I forgot to thank you guys. Thanks Steven and Adrian.

[]'s
Paulo Poiati

blog.paulopoiati.com


On Sun, Aug 19, 2012 at 2:54 PM, Paulo Gabriel Poiati <
paulogpoiati@gmail.com> wrote:

> Steven your solution is great but I have one problem with that when I mock
> the method 'flask.templating._render' the flask-testing extension do not
> record the template rendered through the signal.
>
> Adrian your solutions is clever, creating a wrapper to render the
> templates will resolve my problem at the cost of one more level of
> abstraction just for the sake of testing what in my point of views is
> acceptable.
>
> []'s
> Paulo Poiati
>
> blog.paulopoiati.com
>
>
>
> On Sun, Aug 19, 2012 at 9:11 AM, Adrian Ratnapala <
> adrian.ratnapala@gmail.com> wrote:
>
>> One thing I do is use a wrapper for render_template that remembers
>> what the page and the context are.  The view is rendered as usual.  I
>> can then inspect that record during tests.  I am not sure if this is a
>> solution to your solution, and it is a bit hackish, but it works for
>> me.
>>
>> On 18 August 2012 04:37, Paulo Gabriel Poiati <paulogpoiati@gmail.com>
>> wrote:
>> > Hello all,
>> >
>> > There is a way to not render the real template of the view in the
>> > render_template method? I mean something like render_views in the rspec:
>> >
>> 
https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/controller-specs/render-views
>> > (by default rails not render the views).
>> >
>> > Thanks in advance!
>> >
>> > []'s
>> > Paulo Poiati
>> >
>> > blog.paulopoiati.com
>>
>>
>>
>> --
>> Adrian Ratnapala
>> mobile: +49 1515 277 0000
>>
>> NGBitS GmbH
>> Rainer Strasse 7
>> 83104 Ostermünchen
>>
>> Web: www.ngbits.com
>>
>
>

Re: [flask] Opt to not render template during tests

From:
Paulo Gabriel Poiati
Date:
2012-08-19 @ 18:18
And here is the Steven solution with the signal sent:

import flask
from flask.ext.testing import TestCase
from flask.signals import template_rendered
from dingus import patch
from palimpsesto import app

def _dummy_render(template, context, app):
    """Renders the template and fires the signal"""
    template_rendered.send(app, template=template, context=context)
    return ""

class TestIndex(TestCase):

    def create_app(self):
        return app

    def should_be_success(self):
        response = self.client.get('/')

        self.assert_200(response)

    @patch('flask.templating._render', _dummy_render)
    def should_render_the_index_template(self):
        flask.templating._render.return_value = ""

        self.client.get('/')

        self.assert_template_used('index.jinja2.html')


[]'s
Paulo Poiati

blog.paulopoiati.com


On Sun, Aug 19, 2012 at 2:57 PM, Paulo Gabriel Poiati <
paulogpoiati@gmail.com> wrote:

> Sorry, I forgot to thank you guys. Thanks Steven and Adrian.
>
> []'s
> Paulo Poiati
>
> blog.paulopoiati.com
>
>
>
> On Sun, Aug 19, 2012 at 2:54 PM, Paulo Gabriel Poiati <
> paulogpoiati@gmail.com> wrote:
>
>> Steven your solution is great but I have one problem with that when I
>> mock the method 'flask.templating._render' the flask-testing extension do
>> not record the template rendered through the signal.
>>
>> Adrian your solutions is clever, creating a wrapper to render the
>> templates will resolve my problem at the cost of one more level of
>> abstraction just for the sake of testing what in my point of views is
>> acceptable.
>>
>> []'s
>> Paulo Poiati
>>
>> blog.paulopoiati.com
>>
>>
>>
>> On Sun, Aug 19, 2012 at 9:11 AM, Adrian Ratnapala <
>> adrian.ratnapala@gmail.com> wrote:
>>
>>> One thing I do is use a wrapper for render_template that remembers
>>> what the page and the context are.  The view is rendered as usual.  I
>>> can then inspect that record during tests.  I am not sure if this is a
>>> solution to your solution, and it is a bit hackish, but it works for
>>> me.
>>>
>>> On 18 August 2012 04:37, Paulo Gabriel Poiati <paulogpoiati@gmail.com>
>>> wrote:
>>> > Hello all,
>>> >
>>> > There is a way to not render the real template of the view in the
>>> > render_template method? I mean something like render_views in the
>>> rspec:
>>> >
>>> 
https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/controller-specs/render-views
>>> > (by default rails not render the views).
>>> >
>>> > Thanks in advance!
>>> >
>>> > []'s
>>> > Paulo Poiati
>>> >
>>> > blog.paulopoiati.com
>>>
>>>
>>>
>>> --
>>> Adrian Ratnapala
>>> mobile: +49 1515 277 0000
>>>
>>> NGBitS GmbH
>>> Rainer Strasse 7
>>> 83104 Ostermünchen
>>>
>>> Web: www.ngbits.com
>>>
>>
>>
>

Re: [flask] Opt to not render template during tests

From:
Paulo Gabriel Poiati
Date:
2012-08-19 @ 18:23
I think this is a common pattern for unit testing (isolating the request
handler). What about put some config like "RENDER_VIEWS" or
"TESTING_RENDER_VIEWS" in the core of flask? Or maybe do it in the
flask-testing extension (through monkey patch)?

Thanks.

[]'s
Paulo Poiati

blog.paulopoiati.com


On Sun, Aug 19, 2012 at 3:18 PM, Paulo Gabriel Poiati <
paulogpoiati@gmail.com> wrote:

> And here is the Steven solution with the signal sent:
>
> import flask
> from flask.ext.testing import TestCase
> from flask.signals import template_rendered
> from dingus import patch
> from palimpsesto import app
>
> def _dummy_render(template, context, app):
>     """Renders the template and fires the signal"""
>     template_rendered.send(app, template=template, context=context)
>     return ""
>
> class TestIndex(TestCase):
>
>     def create_app(self):
>         return app
>
>     def should_be_success(self):
>         response = self.client.get('/')
>
>         self.assert_200(response)
>
>     @patch('flask.templating._render', _dummy_render)
>     def should_render_the_index_template(self):
>         flask.templating._render.return_value = ""
>
>         self.client.get('/')
>
>         self.assert_template_used('index.jinja2.html')
>
>
> []'s
> Paulo Poiati
>
> blog.paulopoiati.com
>
>
>
> On Sun, Aug 19, 2012 at 2:57 PM, Paulo Gabriel Poiati <
> paulogpoiati@gmail.com> wrote:
>
>> Sorry, I forgot to thank you guys. Thanks Steven and Adrian.
>>
>> []'s
>> Paulo Poiati
>>
>> blog.paulopoiati.com
>>
>>
>>
>> On Sun, Aug 19, 2012 at 2:54 PM, Paulo Gabriel Poiati <
>> paulogpoiati@gmail.com> wrote:
>>
>>> Steven your solution is great but I have one problem with that when I
>>> mock the method 'flask.templating._render' the flask-testing extension do
>>> not record the template rendered through the signal.
>>>
>>> Adrian your solutions is clever, creating a wrapper to render the
>>> templates will resolve my problem at the cost of one more level of
>>> abstraction just for the sake of testing what in my point of views is
>>> acceptable.
>>>
>>> []'s
>>> Paulo Poiati
>>>
>>> blog.paulopoiati.com
>>>
>>>
>>>
>>> On Sun, Aug 19, 2012 at 9:11 AM, Adrian Ratnapala <
>>> adrian.ratnapala@gmail.com> wrote:
>>>
>>>> One thing I do is use a wrapper for render_template that remembers
>>>> what the page and the context are.  The view is rendered as usual.  I
>>>> can then inspect that record during tests.  I am not sure if this is a
>>>> solution to your solution, and it is a bit hackish, but it works for
>>>> me.
>>>>
>>>> On 18 August 2012 04:37, Paulo Gabriel Poiati <paulogpoiati@gmail.com>
>>>> wrote:
>>>> > Hello all,
>>>> >
>>>> > There is a way to not render the real template of the view in the
>>>> > render_template method? I mean something like render_views in the
>>>> rspec:
>>>> >
>>>> 
https://www.relishapp.com/rspec/rspec-rails/v/2-0/docs/controller-specs/render-views
>>>> > (by default rails not render the views).
>>>> >
>>>> > Thanks in advance!
>>>> >
>>>> > []'s
>>>> > Paulo Poiati
>>>> >
>>>> > blog.paulopoiati.com
>>>>
>>>>
>>>>
>>>> --
>>>> Adrian Ratnapala
>>>> mobile: +49 1515 277 0000
>>>>
>>>> NGBitS GmbH
>>>> Rainer Strasse 7
>>>> 83104 Ostermünchen
>>>>
>>>> Web: www.ngbits.com
>>>>
>>>
>>>
>>
>