Crates.io | axum-test |
lib.rs | axum-test |
version | |
source | src |
created_at | 2023-01-17 23:07:08.733101 |
updated_at | 2025-01-27 20:22:45.28725 |
description | For spinning up and testing Axum servers |
homepage | |
repository | https://github.com/JosephLenton/axum-test |
max_upload_size | |
id | 761391 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This runs your application locally, allowing you to query against it with requests. Decode the responses, and assert what is returned.
use axum::Router;
use axum::routing::get;
use axum_test::TestServer;
#[tokio::test]
async fn it_should_ping_pong() {
// Build an application with a route.
let app = Router::new()
.route(&"/ping", get(|| async { "pong!" }));
// Run the application for testing.
let server = TestServer::new(app).unwrap();
// Get the request.
let response = server
.get("/ping")
.await;
// Assertions.
response.assert_status_ok();
response.assert_text("pong!");
}
A TestServer
enables you to run an Axum service with a mocked network,
or on a random port with real network reqeusts.
In both cases allowing you to run multiple servers, across multiple tests, all in parallel.
Feature | On by default | |
---|---|---|
all |
off | Turns on all features. |
pretty-assertions |
on | Uses the pretty assertions crate on response assert_* methods. |
yaml |
off | Enables support for sending, receiving, and asserting, yaml content. |
msgpack |
off | Enables support for sending, receiving, and asserting, msgpack content. |
shuttle |
off | Enables support for building a TestServer an shuttle_axum::AxumService , for use with Shuttle.rs. |
typed-routing |
off | Enables support for using TypedPath in requests. See axum-extra for details. |
ws |
off | Enables WebSocket support. See TestWebSocket for details. |
reqwest |
off | Enables the TestServer being able to create Reqwest requests for querying. |
The current version of Axum Test requires at least Axum v0.7.6.
Here is a list of compatability with prior versions:
Axum Version | Axum Test Version |
---|---|
0.8.0+ (latest) | 17+ (latest) |
0.7.6 to 0.7.9 | 16 |
0.7.0 to 0.7.5 | 14, 15 |
0.6 | 13.4.1 |
You can find examples of writing tests in the /examples folder. These include tests for:
Querying your application on the TestServer
supports all of the common request building you would expect.
A big thanks to all of these who have helped!
Made with contrib.rocks.