How to test async functions that use Tokio?

Just replace #[test] with #[tokio::test] before any test function. If you use actix-web you can add actix_rt into Cargo.toml and #[actix_rt::test] before the test function

#[tokio::test]
async fn test_something_async() {
    let DB = setup().await; // <- the DB is impl std::future::Future type

    // the DB variable will be used to run another
    // async function named "some_async_func"
    // but it doesn't work since DB is a Future type 
    // Future type need await keyword
    // but if I use await-async keywords here, it complains
    // error: async functions cannot be used for tests
    // so what to do here ?
    some_async_func(DB).await;
}

Leave a Comment