FlaskClient
Works like a regular Werkzeug test client, with additional behavior for Flask. Can 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 :class:werkzeug.test.Client.
Attributes
| Attribute | Type | Description |
|---|---|---|
| application | [Flask](../app/flask.md?sid=flask_app_flask) | The Flask application instance that the client interacts with. |
Constructor
Signature
def FlaskClient(
args: t.Any,
kwargs: t.Any
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| args | t.Any | Positional arguments passed to the superclass constructor. |
| kwargs | t.Any | Keyword arguments passed to the superclass constructor. |
Methods
session_transaction()
@classmethod
def session_transaction(
args: t.Any,
kwargs: t.Any
) - > t.Iterator[[SessionMixin](../sessions/sessionmixin.md?sid=flask_sessions_sessionmixin)]
When used in combination with a with statement this opens a session transaction. This can be used to modify the session that the test client uses. Once the with block is left the session is stored back. Internally 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 :meth:~flask.Flask.test_request_context which are directly passed through.
Parameters
| Name | Type | Description |
|---|---|---|
| args | t.Any | Positional arguments passed directly to :meth:~flask.Flask.test_request_context to configure the temporary request context. |
| kwargs | t.Any | Keyword arguments passed directly to :meth:~flask.Flask.test_request_context to configure the temporary request context. |
Returns
| Type | Description |
|---|---|
t.Iterator[[SessionMixin](../sessions/sessionmixin.md?sid=flask_sessions_sessionmixin)] | An iterator that yields the session object. |
open()
@classmethod
def open(
args: t.Any,
buffered: bool = False,
follow_redirects: bool = False,
kwargs: t.Any
) - > TestResponse
Opens a test request, processes it, and returns a TestResponse. This method simulates a client making a request to the Flask application, handling environment setup, context preservation, and response processing.
Parameters
| Name | Type | Description |
|---|---|---|
| args | t.Any | Positional arguments that can be an EnvironBuilder, a dictionary representing the environment, or a BaseRequest object, or arguments for constructing an EnvironBuilder. |
| buffered | bool = False | A boolean indicating whether the response should be buffered. |
| follow_redirects | bool = False | A boolean indicating whether the client should automatically follow HTTP redirects. |
| kwargs | t.Any | Keyword arguments for constructing an EnvironBuilder if 'args' does not provide a request object directly. |
Returns
| Type | Description |
|---|---|
TestResponse | The TestResponse object representing the application's response to the simulated request. |