Attribute Macro test

Source
#[test]
Expand description

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

§Borrowing the runtime

To borrow the runtime directly, add as a function argument

#[async_local::test(flavor = "multi_thread", worker_threads = 10)]
fn test(runtime: &tokio::runtime::Runtime) {
  runtime.block_on(async {
    assert!(true);
  });
}

§Multi-threaded runtime

To use the multi-threaded runtime, the macro can be configured using

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

The worker_threads option configures the number of worker threads, and defaults to the number of cpus on the system.

Note: The multi-threaded runtime requires the rt-multi-thread feature flag.

§Current thread runtime

The default test runtime is single-threaded. Each test gets a separate current-thread runtime.

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

§Usage

§Using the multi-thread runtime

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

§Using current thread runtime

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

§Set number of worker threads

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

§Configure the runtime to start with time paused

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

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