Quickstart: Run E2E tests with TestServer
mainTo perform end-to-end testing for an Axum application, use TestServer to run your service with either a mocked network or on a random port. You can then use the server instance to build requests (e.g., .get(), .post()) and assert against the responses.
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);
// Get the request.
let response = server
.get("/ping")
.await;
// Assert!
response
.assert_status_ok()
.assert_text("pong!");
}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);
// Get the request.
let response = server
.get("/ping")
.await;
// Assert!
response
.assert_status_ok()
.assert_text("pong!");
}