Flask Test Client API
FlaskClient is a simulated browser that can send requests and verify responses without starting a server.
Create an instance via app.test_client().
Create a test client
| Method | Description |
|---|---|
| app.test_client(use_cookies=True) | Standard way of creating it. use_cookies=False disables Cookie handling |
| with app.test_client() as client: | Context manager. Automatically cleans up the request context on exit |
Request methods
All methods return a TestResponse object (inheriting from Response).
| Method | Description |
|---|---|
| get(path, **kwargs) | Send a GET request |
| post(path, **kwargs) | Send a POST request |
| put(path, **kwargs) | Send a PUT request |
| delete(path, **kwargs) | Send a DELETE request |
| patch(path, **kwargs) | Send a PATCH request |
| open(path, method, **kwargs) | Generic request method; method must be specified explicitly |
Request parameters (kwargs)
| Parameters | Description |
|---|---|
| data | Request body (string or bytes). Used for form submission |
| json | JSON data, automatically serialized with Content-Type: application/json set |
| query_string | URL query parameters as a string or dictionary |
| headers | Request headers dictionary |
| content_type | Custom Content-Type |
| follow_redirects | bool, whether to automatically follow redirects. Defaults to False |
| buffered | bool, whether to buffer the response. Defaults to False |
Response object (TestResponse)
| Attribute / method | Description |
|---|---|
| status_code | HTTP status code (int) |
| status | HTTP status text, such as "200 OK" |
| data | Response body (bytes) |
| text | Get the response body as text (str) |
| get_json() | Parse the response body as JSON. Returns a dict or list |
| headers | Response headers (Headers object) |
| location | The redirect target URL (only for 3xx responses) |
| json_module | The module used for JSON serialization (defaults to app.json) |
Session testing
| Method | Description |
|---|---|
| session_transaction(**kwargs) | Context manager for reading and writing the session directly. Returns a SessionMixin object |
Code Example
Example
from
app
import
create_app
app
=
create_app
(
)
app.
config
[
"TESTING"
]
=
True
def
test_api
(
)
:
with
app.
test_client
(
)
as
client:
GET requests
resp
=
client.
get
(
"/api/posts"
)
assert
resp.
status_code
==
200
data
=
resp.
get_json
(
)
assert
isinstance
(
data
,
list
)
POST JSON request
resp
=
client.
post
(
"/api/posts"
,
json
=
{
"title"
:
"RUNOOB tutorial"
,
"body"
:
"Getting started content"
}
)
assert
resp.
status_code
==
201
assert
resp.
get_json
(
)
[
"title"
]
==
"RUNOOB tutorial"
A request with query parameters
resp
=
client.
get
(
"/search?q=flask"
)
assert
b
"flask"
in
resp.
data
Simulate a logged-in state
with
client.
session_transaction
(
)
as
sess:
sess
[
"username"
]
=
"runoob"
resp
=
client.
get
(
"/dashboard"
)
assert
b
"runoob"
in
resp.
data
Follow redirects
resp
=
client.
post
(
"/login"
,
data
=
{
"username"
:
"admin"
}
,
follow_redirects
=
True
)
assert
resp.
status_code
==
200