Crates.io | axum-test |
lib.rs | axum-test |
version | 15.3.0 |
source | src |
created_at | 2023-01-17 23:07:08.733101 |
updated_at | 2024-07-06 11:10:05.405099 |
description | For spinning up and testing Axum servers |
homepage | |
repository | https://github.com/JosephLenton/axum-test |
max_upload_size | |
id | 761391 |
size | 342,687 |
Using this library, you can host your application and query against it with requests. Then decode the responses, and assert what is returned.
use ::axum::Router;
use ::axum::routing::get;
use ::axum_test::TestServer;
async fn get_ping() -> &'static str {
"pong!"
}
#[tokio::test]
async fn it_should_get() {
// Build an application with a route.
let app = Router::new()
.route("/ping", get(get_ping));
// Run the application for testing.
let server = TestServer::new(app).unwrap();
// Get the request.
let response = server
.get("/ping")
.await;
assert_eq!(response.text(), "pong!");
}
The TestServer
can run requests directly against your application with a mocked network,
or the application can run on a random port (with real network reqeusts being made).
In both cases allowing multiple servers to run in parallel, across your tests.
This behaviour can be changed in the TestServerConfig
, by selecting the transport
to be used.
Axum Test requires the latest version of Axum (0.7).
Axum Version | Axum Test Version |
---|---|
0.7 (latest) | 14, 15+ (latest) |
0.6 | 13.4.1 |
Here are a list of all features so far that can be enabled:
all
off by default, turns on all features below.pretty-assertions
on by default, uses the pretty assertions crate for the output to the assert_*
functions.yaml
off by default, adds support for sending, receiving, and asserting, yaml content.msgpack
off by default, adds support for sending, receiving, and asserting, msgpack content.typed-routing
off by default, adds support for the TypedPath
from axum-extra.ws
off by default, adds support for WebSockets.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.