Re: [flask] Using url_for in tests (outside of request context)
- From:
- Vladimir Mihailenco
- Date:
- 2011-08-25 @ 15:09
Many thanks Ron, I will try Flask-Testing.
On Thu, Aug 25, 2011 at 4:37 PM, Ron DuPlain <ron.duplain@gmail.com> wrote:
> On Thu, Aug 25, 2011 at 5:46 AM, Vladimir Mihailenco
> <vladimir.webdev@gmail.com> wrote:
>> I am using url_for() in my model pre_save() hook to generate some
>> urls. There is no errors in production, but in tests I am getting:
>>
>> RuntimeError: working outside of request context
>>
>> There is workaround like this:
>>
>> with app.test_request_context('/'):
>> user = User(name='name').save()
>>
>> but I think this is far from being ideal... What else can I do? Thanks.
>
> The Flask-Testing extension provides an extension to unittest which
> does the request context work for you.
>
> You can also setup with:
>
> ctx = app.test_request_context()
> ctx.push()
>
> Do your tests. Teardown with:
>
> ctx.pop()
>
> This is discussed here:
> http://flask.pocoo.org/docs/reqcontext/
>
> -Ron
>