Test Client ​
class flask.testing.FlaskClient(*args, **kwargs) ​
Works like a regular Werkzeug test client but has knowledge about Flask’s contexts to defer the cleanup of the request context until the end of a with block. For general information about how to use this class refer to werkzeug.test.Client.
Changelog
Changed in version 0.12: app.test_client() includes preset default environment, which can be set after instantiation of the app.test_client() object in client.environ_base.
Basic usage is outlined in the Testing Flask Applications chapter.
Parameters:
args (t.Any)–kwargs (t.Any)–
open(
*args, buffered=False, follow_redirects=False, **kwargs) ​Generate an environ dict from the given arguments, make a request to the application using it, and return the response.
Parameters:
args (t.Any)– Passed toEnvironBuilderto create the environ for the request. If a single arg is passed, it can be an existingEnvironBuilderor an environ dict.buffered (bool)– Convert the iterator returned by the app into a list. If the iterator has aclose()method, it is called automatically.follow_redirects (bool)– Make additional requests to followHTTPredirects until a non-redirect status is returned.TestResponse.historylists the intermediate responses.kwargs (t.Any)–
Return type:
TestResponseChangelog
Changed in version 2.1: Removed the
as_tupleparameter.Changed in version 2.0: The request input stream is closed when calling
response.close(). Input streams for redirects are automatically closed.Changed in version 0.5: If a dict is provided as file in the dict for the data parameter the content type has to be called
content_typeinstead ofmimetype. This change was made for consistency withwerkzeug.FileWrapper.Changed in version 0.5: Added the
follow_redirectsparameter.session_transaction(
*args, **kwargs) ​When used in combination with a
withstatement this opens a session transaction. This can be used to modify the session that the test client uses. Once thewithblock is left the session is stored back.pythonwith client.session_transaction() as session: session['value'] = 42with client.session_transaction() as session: session['value'] = 42Internally this is implemented by going through a temporary test request context and since session handling could depend on request variables this function accepts the same arguments as
test_request_context()which are directly passed through.Parameters:
args (Any)–kwargs (Any)–
Return type:
Generator[SessionMixin, None, None]