While doing the Miguel Grinberg's Flask Web Development, I got stuck while testing the gravatar code,
def test_gravatar(self):
u = User(email='[email protected]', password='cat')
with self.app.test_request_context('/'):
gravatar = u.gravatar()
gravatar_256 = u.gravatar(size=256)
gravatar_pg = u.gravatar(rating='pg')
gravatar_retro = u.gravatar(default='retro')
with self.app.test_request_context('/', base_url='https://example.com'):
gravatar_ssl = u.gravatar()
self.assertTrue('http://www.gravatar.com/avatar/' +
'd4c74594d841139328695756648b6bd6'in gravatar)
self.assertTrue('s=256' in gravatar_256)
self.assertTrue('r=pg' in gravatar_pg)
self.assertTrue('d=retro' in gravatar_retro)
self.assertTrue('https://secure.gravatar.com/avatar/' +
'd4c74594d841139328695756648b6bd6' in gravatar_ssl)
What does app.test_request_context() do and how is it different from app_context()?
Why do we even need to call with self.app.test_request_context('/')? Also, what changes can we do to shift the call to app.test_request_context() in SetUp()?
Flask provides a way to test your application by exposing the Werkzeug test Clientand handling the context locals for you. You can then use that with your favourite testing solution. In this documentation we will use the pytestpackage as the base framework for our tests.
The code those tests cover is likely where the bug is hiding. Python Flask is a framework that makes it easy to create web apps with Python. This guide will use a Flask app as an example and walk you through creating unit tests for it. Even if you don’t use Flask, the unit-testing concepts illustrated are generally applicable.
Flask provides a way to test your application by exposing the Werkzeug test Clientand handling the context locals for you. You can then use that with your favourite testing solution. In this documentation we will use the pytestpackage as the base framework for our tests. You can install it with pip, like so: $ pip install pytest The Application¶
Both app and request contexts can also be pushed manually, which is useful when using the interpreter. Flask-Script or the new Flask cli will automatically push an app context when running the shell command. Flask-Testing is a useful library that contains helpers for testing Flask apps.
There's plenty of reading to do on the subject, so start with the documentation: app_context
, test_request_context
, and you can always double-check the code: app_context
and test_request_context
. In addition, here's an article discussion Flask's contexts.
That's a lot of links, so for a break-down:
We can see that app_context
creates a new application context, while test_request_context
creates a new request context. Application contexts are created in two situations: manually with app_context
and when a request context is created, which, in turn, is created with test_request_context
or at the beginning of the request.
So when a request comes into your application, a RequestContext is created. The creation of this object creates an application context.
Why test_request_context
? You need that context to access the application when working outside of a context created by a request, like proxies that you probably recognize, like current_app
, request
, g
, and session
. Going down into the code, when you create a RequestContext with test_request_context
instead of request_context
, you're getting a EnvironBuilder
object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With