Sometimes, we want to test code that requires a Python Flask app or request context.
In this article, we’ll look at how to test code that requires a Python Flask app or request context.
How to test code that requires a Python Flask app or request context?
To test code that requires a Python Flask app or request context, we can call test_client
to return a test version of the app.
For instance, we write
c = app.test_client()
response = c.get('/test/url')
to call test_client
on the Flask app
.
And then we call get
to make a GET request.
We get the app context with
with app.app_context():
# test code
And we get the request context with:
with current_app.test_request_context():
# test code
Conclusion
To test code that requires a Python Flask app or request context, we can call test_client
to return a test version of the app.