flask.Flask.test_client¶
-
Flask.test_client(use_cookies=True, **kwargs)[source]¶ Creates a test client for this application. For information about unit testing head over to 1 Testing Flask Applications.
Note that if you are testing for assertions or exceptions in your application code, you must set
app.testing = Truein order for the exceptions to propagate to the test client. Otherwise, the exception will be handled by the application (not visible to the test client) and the only indication of an AssertionError or other exception will be a 500 status code response to the test client. See thetestingattribute. For example:app.testing = True client = app.test_client()
The test client can be used in a
withblock to defer the closing down of the context until the end of thewithblock. This is useful if you want to access the context locals for testing:with app.test_client() as c: rv = c.get('/?vodka=42') assert request.args['vodka'] == '42'
Additionally, you may pass optional keyword arguments that will then be passed to the application’s
test_client_classconstructor. For example:from flask.testing import FlaskClient class CustomClient(FlaskClient): def __init__(self, authentication=None, *args, **kwargs): FlaskClient.__init__(*args, **kwargs) self._authentication = authentication app.test_client_class = CustomClient client = app.test_client(authentication='Basic ....')
See
FlaskClientfor more information.Changed in version 0.4: added support for
withblock usage for the client.New in version 0.7: The use_cookies parameter was added as well as the ability to override the client to be used by setting the
test_client_classattribute.Changed in version 0.11: Added **kwargs to support passing additional keyword arguments to the constructor of
test_client_class.