Testing API

TestClient

Synchronous test client.

Constructor

TestClient(app: Any, base_url: str = "http://testserver")

Methods

  • get(path, **kwargs) — Send a GET request
  • post(path, **kwargs) — Send a POST request
  • put(path, **kwargs) — Send a PUT request
  • patch(path, **kwargs) — Send a PATCH request
  • delete(path, **kwargs) — Send a DELETE request
  • options(path, **kwargs) — Send an OPTIONS request
  • head(path, **kwargs) — Send a HEAD request
  • request(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 request
  • async post(path, **kwargs) — Send a POST request
  • async put(path, **kwargs) — Send a PUT request
  • async patch(path, **kwargs) — Send a PATCH request
  • async delete(path, **kwargs) — Send a DELETE request
  • async options(path, **kwargs) — Send an OPTIONS request
  • async head(path, **kwargs) — Send a HEAD request
  • async 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 endpoint
  • async disconnect(code=1000) — Disconnect from the WebSocket
  • async send_text(text) — Send a text message
  • async send_bytes(data) — Send a binary message
  • async send_json(data) — Send a JSON message
  • async receive_text() — Receive a text message
  • async receive_json() — Receive a JSON message

TestResponse

Test response.

Attributes

  • status_code — HTTP status code
  • headers — Response headers
  • content — Response content

Properties

  • text — Get response as text
  • json() — Get response as JSON

Assertions

Test assertions.

  • assert_status(response, expected) — Assert HTTP status code
  • assert_json(response) — Assert response is JSON
  • assert_json_array(response) — Assert response is a JSON array
  • assert_has_key(data, key) — Assert dictionary has key
  • assert_key_value(data, key, expected) — Assert key has expected value
  • assert_success(data) — Assert success response
  • assert_error(data) — Assert error response
  • assert_error_code(data, code) — Assert error code
  • assert_validation_error(data, field=None) — Assert validation error
  • assert_redirect(response, expected_location=None) — Assert redirect
  • assert_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")