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 operationsMacros§
- 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§
- Cancellation
Test - Helper for testing handler cancellation behavior.
- Cancellation
Test Result - Result of a cancellation test.
- Captured
Log - A captured log entry for test assertions.
- Cookie
Jar - 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.
- Fixture
Guard - A guard that automatically calls teardown when dropped.
- Integration
Test - Context for integration tests that manages fixtures and test client.
- Integration
Test Context - Context available during an integration test.
- LabTest
Config - Configuration for Lab-based deterministic testing.
- LogCapture
- Result of a log capture operation.
- Mock
Response - Configuration for a canned response.
- Mock
Server - A mock HTTP server for integration testing.
- Mock
Server Options - Options for configuring a MockServer.
- Mock
Time - Virtual time utilities for testing timeouts and delays.
- Recorded
Request - A recorded request from the mock server.
- Request
Builder - Builder for constructing test requests with a fluent API.
- Response
Diff - Request/response diff helper for test assertions.
- Response
Snapshot - A serializable snapshot of an HTTP response for fixture-based testing.
- Test
Chaos Stats - Statistics about chaos injection during a test.
- Test
Client - Test client for in-process HTTP testing.
- Test
Logger - Test logger that captures logs for per-test isolation and assertions.
- Test
Response - Response from a test request with assertion helpers.
- Test
Server - A real HTTP test server that routes requests through the full App pipeline.
- Test
Server Config - Configuration for
TestServer. - Test
Server LogEntry - A log entry recorded by
TestServerfor each processed request. - Test
Timings - Timing breakdown for test phases.
Enums§
- E2EStep
Result - Result of executing an E2E step.
Traits§
- Into
Status U16 - Helper trait to convert various types to u16 for status code comparison.
- Test
Fixture - Trait for test fixtures that set up and tear down test data.
Functions§
- json_
contains - Checks if
actualcontains all fields fromexpected.