FlaskClient
A test client for making requests to your Flask application. Works like a regular Werkzeug test client, with additional behavior for Flask.Creating a Test Client
Useapp.test_client() to create a test client:
Whether the client should handle cookies. Set to
False to disable cookie handling.Additional keyword arguments passed to the
FlaskClient constructor. Useful when using a custom test client class.FlaskClient instance.
Making Requests
open
Can be a URL path, an
EnvironBuilder, a WSGI environment dict, or a Request object.Whether to buffer the response data.
Whether to follow HTTP redirects automatically.
Additional arguments to build the request:
method: HTTP method (GET, POST, etc.)data: Request body dataquery_string: URL query parametersheaders: Request headersjson: JSON data (sets content-type automatically)content_type: Content-Type header
TestResponse object.
HTTP Method Helpers
Convenience methods for common HTTP methods:open().
Session Management
session_transaction
Arguments passed to
test_request_context().Keyword arguments passed to
test_request_context().Context Preservation
Use the client as a context manager to preserve the request context:Attributes
Default environment variables for all requests. Set after creating the client:Default values:
REMOTE_ADDR:"127.0.0.1"HTTP_USER_AGENT:"Werkzeug/{version}"
Whether to preserve the request context. Automatically set to
True when using the client as a context manager.Version History
- Changed in version 0.12:
app.test_client()includes preset default environment, which can be set after instantiation inclient.environ_base. - Changed in version 0.4: Added support for
withblock usage.
FlaskCliRunner
A CLI runner for testing Flask CLI commands. Based on Click’sCliRunner.
Creating a CLI Runner
Useapp.test_cli_runner() to create a CLI runner:
Keyword arguments passed to Click’s
CliRunner constructor.FlaskCliRunner instance.
Running Commands
invoke
Command object to invoke. Defaults to the app’s
cli group.List of strings to invoke the command with, or a single string that will be split.
Additional keyword arguments:
input: Input string for interactive commandsenv: Environment variables dictcatch_exceptions: Whether to catch exceptions (default: True)color: Whether to use ANSI colorsmix_stderr: Whether to mix stderr into stdout
Result object with:
exit_code: The command’s exit codeoutput: The command’s outputexception: The exception raised (if any)exc_info: Exception info tuple
obj argument is not given, passes an instance of flask.cli.ScriptInfo that knows how to load the Flask app being tested.
Version History
Added in version 1.0
EnvironBuilder
A helper for building WSGI environments with defaults from the Flask application.Constructor
The Flask application to configure the environment from.
URL path being requested.
Base URL where the app is being served, which
path is relative to. If not given, built from PREFERRED_URL_SCHEME, subdomain, SERVER_NAME, and APPLICATION_ROOT.Subdomain name to append to
SERVER_NAME. Cannot be used with base_url.Scheme to use instead of
PREFERRED_URL_SCHEME. Cannot be used with base_url.Additional positional arguments passed to
werkzeug.test.EnvironBuilder.Additional keyword arguments passed to
werkzeug.test.EnvironBuilder:method: HTTP methoddata: Request bodyquery_string: URL query parametersheaders: Request headersjson: JSON data to serialize
Methods
json_dumps
obj to a JSON-formatted string using the app’s JSON configuration.
The object to serialize.
Additional arguments passed to the JSON encoder.
Testing Examples
Basic Testing
Testing JSON APIs
Testing with Session
Testing with Context
Testing CLI Commands
Testing File Uploads
Testing Redirects
Custom Test Client
See Also
- Testing Flask Applications - Complete testing guide
- Pytest documentation - Popular testing framework
- Click Testing - Click’s testing utilities
