
#[cfg(test)]
Learn what the cfg(test) attribute does, how it keeps unit tests out of normal builds, and how to write a tests module in Rust
Pure function unit tests
Learn what a pure function is, why pure functions are the easiest code to test, and how to write unit tests for them in Rust
Validation helpers
Learn how to write small reusable validation helper functions that check one rule each and return AppError ValidationError for bad input
tower ServiceExt
Learn what the tower ServiceExt trait adds to every service and how its oneshot ready and adapter methods let tests call a Router without a network socket
tower oneshot
Learn how tower oneshot sends exactly one request through an Axum Router in a test and returns the response without starting a server
Test request builder
Learn how Request builder builds the exact HTTP request a test sends through oneshot with its method URI headers and body set piece by piece
Response status assertions
Learn how to assert on the HTTP status of an Axum test response with StatusCode constants class helpers and the full status table for the book app
JSON body assertions
Learn how to read an Axum test response body with to_bytes parse it with serde_json and assert on fields whole values arrays and error bodies
Test file layout
Learn how to lay out Rust tests for an Axum app with unit tests in src integration tests in tests and one shared helpers module
Test database setup
Learn how to set up a separate Postgres test database for Axum handler tests, run migrations before tests, and keep test data away from development data
sqlx test
Learn how the sqlx test macro gives each test its own fresh Postgres database, runs migrations, applies fixtures, and cleans up afterwards
Docker Postgres for tests
Learn how to run Postgres in a Docker container for Axum tests, what each part of the run command does, and how the testcontainers crate starts the same container from Rust
Create-then-fetch flow
Learn how to test a create handler and a read handler together by posting a user, reading it back by id, and checking the two responses match
Transaction rollback strategy
Learn how to isolate database tests by wrapping each test in a transaction and rolling it back, so test rows never reach the shared test database
The tokio test attribute
Learn what tokio test does, how it runs async test functions, and how its flavor and paused clock options change the runtime each test gets
Seeding fixture data
Learn how to seed known test users into app state before each test, why each test gets fresh data, and how fixtures keep tests isolated and repeatable
Authenticated request in tests
Learn how to send a Bearer token with a test request, how to mint a valid token in the test itself, and how to check a protected route answers with the right user
Protected route 401 assertion
Learn how to assert every way a protected route can answer 401, why the status and the body must both be checked, and how 401 differs from 403
WebSocket endpoint test client
Learn how to test a WebSocket endpoint with a real client, why oneshot cannot do the handshake, and how to assert echo round trips and authenticated upgrades
SSE stream response test
Learn how to test a server-sent events endpoint, how to read its stream body in a test, and how to assert the wire format and Last-Event-ID resume
Mocking external HTTP services
Learn how to replace a real external API with a wiremock server in tests, how to stub its answers, and how to check what your code actually sent it