Attribute Macro async_entry::test

source · []
#[test]
Expand description

Marks async function to be executed by runtime, suitable to test environment

Usage

Multi-thread runtime

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn my_test() {
    assert!(true);
}

Using default

The default test runtime is single-threaded.

#[tokio::test]
async fn my_test() {
    assert!(true);
}

Configure the runtime to start with time paused

#[tokio::test(start_paused = true)]
async fn my_test() {
    assert!(true);
}

Note that start_paused requires the test-util feature to be enabled.

Add initialization statement

#[tokio::test(init = "init_log!()")]
async fn my_test() {
    assert!(true);
}
// Will produce:
//
// fn my_test() {
//
//     let _g = init_log!();  // Add init statement
//
//     let body = async { assert!(true); };
//     let rt = ...
//     rt.block_on(body);
// }

Add tracing span over the test fn

#[tokio::test(tracing_span = "info")]
async fn my_test() {
    assert!(true);
}
// Will produce:
//
// fn my_test() {
//     let body = async { assert!(true); };
//
//     use tracing_futures::Instrument;                 // Add tracing span
//     let body_span = tracing::info_span("my_test");   //
//     let body = body.instrument(body_span);           //
//
//     let rt = ...
//     rt.block_on(body);
// }

NOTE:

If you rename the Tokio crate in your dependencies this macro will not work. If you must rename the current version of Tokio because you’re also using an older version of Tokio, you must make the current version of Tokio available as tokio in the module where this macro is expanded.