73 stable releases (14 major)

new 16.4.0 Nov 15, 2024
16.1.0 Sep 29, 2024
15.7.4 Sep 23, 2024
15.3.0 Jul 6, 2024
2.0.0 Jan 17, 2023

#7 in HTTP server

Download history 17997/week @ 2024-07-27 18024/week @ 2024-08-03 18498/week @ 2024-08-10 21151/week @ 2024-08-17 20172/week @ 2024-08-24 22218/week @ 2024-08-31 19947/week @ 2024-09-07 19907/week @ 2024-09-14 23804/week @ 2024-09-21 23492/week @ 2024-09-28 27663/week @ 2024-10-05 31207/week @ 2024-10-12 30456/week @ 2024-10-19 32761/week @ 2024-10-26 35328/week @ 2024-11-02 25239/week @ 2024-11-09

129,007 downloads per month
Used in 38 crates

MIT license

390KB
9K SLoC

Axum Test

Easy E2E testing for Axum
including REST, WebSockets, and more

crate docs


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.

Crate Features

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.

Axum Compatability

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.7.6 (latest) 16+ (latest)
0.7 14, 15
0.6 13.4.1

Examples

You can find examples of writing tests in the /examples folder. These include tests for:

Request Building Features

Querying your application on the TestServer supports all of the common request building you would expect.

  • Serializing and deserializing Json, Form, Yaml, and others, using Serde
  • Assertions on the Json, text, Yaml, etc, that is returned.
  • Cookie, query, and header setting and reading
  • Status code reading and assertions

Also includes

  • WebSockets testing support
  • Saving returned cookies for use on future requests
  • Setting headers, query, and cookies, globally for all requests or on per request basis
  • Can run requests using a real web server, or with mocked HTTP
  • Automatic status assertions for expecting requests to succeed (to help catch bugs in tests sooner)
  • Prettified assertion output
  • Typed Routing from Axum Extra
  • Reqwest integration

Contributions

A big thanks to all of these who have helped!

Made with contrib.rocks.

Dependencies

~10–24MB
~357K SLoC