Skip to main content

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

AttributeTypeDescription
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

NameTypeDescription
argst.AnyPositional arguments passed to the superclass constructor.
kwargst.AnyKeyword 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

NameTypeDescription
argst.AnyPositional arguments passed directly to :meth:~flask.Flask.test_request_context to configure the temporary request context.
kwargst.AnyKeyword arguments passed directly to :meth:~flask.Flask.test_request_context to configure the temporary request context.

Returns

TypeDescription
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

NameTypeDescription
argst.AnyPositional arguments that can be an EnvironBuilder, a dictionary representing the environment, or a BaseRequest object, or arguments for constructing an EnvironBuilder.
bufferedbool = FalseA boolean indicating whether the response should be buffered.
follow_redirectsbool = FalseA boolean indicating whether the client should automatically follow HTTP redirects.
kwargst.AnyKeyword arguments for constructing an EnvironBuilder if 'args' does not provide a request object directly.

Returns

TypeDescription
TestResponseThe TestResponse object representing the application's response to the simulated request.