How to Write Async Tests in Rust

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, requiring the tokio crate with the test-util feature. Add tokio = { version = "1", features = ["full"] } to your Cargo.toml and mark your async test function with the attribute.

#[tokio::test]
async fn async_test_example() {
    let result = 2 + 2;
    assert_eq!(result, 4);
}