How to Write Async Tests in Rust (with tokio

:test)

Write async tests in Rust by adding the tokio dependency and using the #[tokio::test] attribute on your async functions.

Use the #[tokio::test] attribute to run async functions as tests, which automatically spawns a Tokio runtime. Add tokio with the full feature to your Cargo.toml dependencies, then mark your async test function with the attribute.

#[tokio::test]
async fn my_async_test() {
    let result = 2 + 2;
    assert_eq!(result, 4);
}
[dev-dependencies]
tokio = { version = "1", features = ["full"] }