47 stable releases (12 major)

14.8.0 Apr 3, 2024
14.7.0 Mar 29, 2024
14.3.1 Feb 25, 2024
14.2.2 Dec 31, 2023
2.0.0 Jan 17, 2023

#12 in HTTP server

Download history 3698/week @ 2023-12-23 5227/week @ 2023-12-30 6405/week @ 2024-01-06 6248/week @ 2024-01-13 7592/week @ 2024-01-20 7462/week @ 2024-01-27 7832/week @ 2024-02-03 7816/week @ 2024-02-10 8073/week @ 2024-02-17 9249/week @ 2024-02-24 9506/week @ 2024-03-02 8247/week @ 2024-03-09 8077/week @ 2024-03-16 8049/week @ 2024-03-23 7307/week @ 2024-03-30 6193/week @ 2024-04-06

30,893 downloads per month
Used in 21 crates

MIT license

215KB
4.5K SLoC

Axum Test

Easy E2E testing for applications built on Axum

crate docs


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 Compatability

Axum Test requires the latest version of Axum (0.7).

Axum Version Axum Test Version
0.7 (latest) 14+ (latest)
0.6 13.4.1

Example

You can find a thorough example in the /examples folder.

Request building

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

  • Serializing and deserializing Json and Form content using Serde
  • Cookie setting and reading
  • Access to setting and reading headers
  • Status code reading and assertions
  • Assertions for defining what you expect to have returned

It also includes

  • Saving cookies returned for use across future requests.
  • Setting headers and query parameters for use across all TestRequests.
  • Can optionally run requests using a real web server.
  • Automatic status assertions for checking requests always succeed or fail.
  • Prettifying the assertion output.

Crate Features

Here are a list of all features so far that can be enabled:

  • 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.

Dependencies

~9–13MB
~256K SLoC