Skip to main content

Module testing

Module testing 

Source
Expand description

Test utilities for fastapi applications.

This module provides a TestClient for in-process testing of handlers without network overhead. It integrates with asupersync’s capability model and supports deterministic testing via the Lab runtime.

§Features

  • In-process testing: No network I/O, fast execution
  • HTTP-like API: Familiar client.get("/path") interface
  • Request builder: Fluent API for headers, body, cookies
  • Response assertions: Convenient assertion helpers
  • Cookie jar: Automatic session management across requests
  • Lab integration: Deterministic testing with asupersync

§Example

use fastapi_core::testing::TestClient;
use fastapi_core::middleware::Handler;

async fn hello_handler(ctx: &RequestContext, req: &mut Request) -> Response {
    Response::ok().body(ResponseBody::Bytes(b"Hello, World!".to_vec()))
}

#[test]
fn test_hello() {
    let client = TestClient::new(hello_handler);
    let response = client.get("/hello").send();

    assert_eq!(response.status().as_u16(), 200);
    assert_eq!(response.text(), "Hello, World!");
}

§Deterministic Testing

For reproducible tests involving concurrency, use TestClient::with_seed:

let client = TestClient::with_seed(handler, 42);
// Same seed = same execution order for concurrent operations

Macros§

assert_eq_with_logs
Assertion helper that includes log context for equality checks.
assert_with_logs
Assertion helper that includes log context on failure.
e2e_test
Macro for defining E2E test scenarios with a declarative syntax.

Structs§

CancellationTest
Helper for testing handler cancellation behavior.
CancellationTestResult
Result of a cancellation test.
CapturedLog
A captured log entry for test assertions.
CookieJar
A simple cookie jar for maintaining cookies across requests.
E2ECapture
A captured HTTP request/response pair from an E2E step.
E2EReport
E2E test report with multiple output formats.
E2EScenario
E2E test scenario builder and executor.
E2EStep
A single step in an E2E test scenario.
FixtureGuard
A guard that automatically calls teardown when dropped.
IntegrationTest
Context for integration tests that manages fixtures and test client.
IntegrationTestContext
Context available during an integration test.
LabTestConfig
Configuration for Lab-based deterministic testing.
LogCapture
Result of a log capture operation.
MockResponse
Configuration for a canned response.
MockServer
A mock HTTP server for integration testing.
MockServerOptions
Options for configuring a MockServer.
MockTime
Virtual time utilities for testing timeouts and delays.
RecordedRequest
A recorded request from the mock server.
RequestBuilder
Builder for constructing test requests with a fluent API.
ResponseDiff
Request/response diff helper for test assertions.
ResponseSnapshot
A serializable snapshot of an HTTP response for fixture-based testing.
TestChaosStats
Statistics about chaos injection during a test.
TestClient
Test client for in-process HTTP testing.
TestLogger
Test logger that captures logs for per-test isolation and assertions.
TestResponse
Response from a test request with assertion helpers.
TestServer
A real HTTP test server that routes requests through the full App pipeline.
TestServerConfig
Configuration for TestServer.
TestServerLogEntry
A log entry recorded by TestServer for each processed request.
TestTimings
Timing breakdown for test phases.

Enums§

E2EStepResult
Result of executing an E2E step.

Traits§

IntoStatusU16
Helper trait to convert various types to u16 for status code comparison.
TestFixture
Trait for test fixtures that set up and tear down test data.

Functions§

json_contains
Checks if actual contains all fields from expected.