1 unstable release

0.1.0 Dec 9, 2021

#1269 in Encoding

MIT/Apache

22KB
171 lines

restest

Black-box integration test for REST APIs in Rust.

This crate provides the assert_api macro that allows to declaratively test, given a certain request, that the response emitted by the server is correct.

Example

#![feature(assert_matches)]

use serde::{Deserialize, Serialize};

restest::port! { 8080 }

restest::assert_api! {
    POST "/user",
    PostUser {
        year_of_birth: 2000,
    } => User {
        year_of_birth: 2000,
        ..
    }
}

#[derive(Debug, Serialize)]
struct PostUser {
    year_of_birth: usize,
}

#[derive(Debug, Deserialize)]
struct User {
    year_of_birth: usize,
    id: Uuid
}

Writing tests

The port macro sets the port at which the request must be run.

The tests are written as normal Rust tests (ie: functions annotated with #[test]). As we're using asynchronous code, we must write async tests, perhaps using #[tokio::test].

More specifically, the assert_api macro can be used in order to query the server API and analyze its response.

Running tests

The server must be running in the background when cargo test is run.

Required toolchain

The nightly feature allows the assert_api macro to expand to nightly-specific code. This offers the following features:

  • better panic message when the request body does not match the expected pattern (requires assert_matches),
  • ability to reuse variables matched in the response body (requires let_else) (still WIP).

These two features must be added at the crate root using the following two lines:

#![feature(assert_matches)]
#![feature(let_else)]

Code of Conduct

We have a Code of Conduct so as to create a more enjoyable community and work environment. Please see the CODE_OF_CONDUCT file for more details.

License

Licensed under either of

at your option.

Dual MIT/Apache2 is strictly more permissive

Dependencies

~4–17MB
~251K SLoC