Testing API
TestClient
Synchronous test client.
Constructor
TestClient(app: Any, base_url: str = "http://testserver")
Methods
get(path, **kwargs)— Send a GET requestpost(path, **kwargs)— Send a POST requestput(path, **kwargs)— Send a PUT requestpatch(path, **kwargs)— Send a PATCH requestdelete(path, **kwargs)— Send a DELETE requestoptions(path, **kwargs)— Send an OPTIONS requesthead(path, **kwargs)— Send a HEAD requestrequest(method, path, **kwargs)— Send a custom request
AsyncTestClient
Asynchronous test client.
Constructor
AsyncTestClient(app: Any, base_url: str = "http://testserver")
Methods
async get(path, **kwargs)— Send a GET requestasync post(path, **kwargs)— Send a POST requestasync put(path, **kwargs)— Send a PUT requestasync patch(path, **kwargs)— Send a PATCH requestasync delete(path, **kwargs)— Send a DELETE requestasync options(path, **kwargs)— Send an OPTIONS requestasync head(path, **kwargs)— Send a HEAD requestasync request(method, path, **kwargs)— Send a custom request
WebSocketClient
Synchronous WebSocket client.
Constructor
WebSocketClient(app: Any, base_url: str = "ws://testserver")
Methods
async connect(path, headers=None)— Connect to a WebSocket endpointasync disconnect(code=1000)— Disconnect from the WebSocketasync send_text(text)— Send a text messageasync send_bytes(data)— Send a binary messageasync send_json(data)— Send a JSON messageasync receive_text()— Receive a text messageasync receive_json()— Receive a JSON message
TestResponse
Test response.
Attributes
status_code— HTTP status codeheaders— Response headerscontent— Response content
Properties
text— Get response as textjson()— Get response as JSON
Assertions
Test assertions.
assert_status(response, expected)— Assert HTTP status codeassert_json(response)— Assert response is JSONassert_json_array(response)— Assert response is a JSON arrayassert_has_key(data, key)— Assert dictionary has keyassert_key_value(data, key, expected)— Assert key has expected valueassert_success(data)— Assert success responseassert_error(data)— Assert error responseassert_error_code(data, code)— Assert error codeassert_validation_error(data, field=None)— Assert validation errorassert_redirect(response, expected_location=None)— Assert redirectassert_header(response, key, expected=None)— Assert header
Example
from flaxon.testing import TestClient, Assertions
def test_route():
client = TestClient(app)
response = client.get("/")
Assertions.assert_status(response, 200)
data = Assertions.assert_json(response)
Assertions.assert_has_key(data, "message")