如何测试使用 Tokio 的异步函数?
How to test async functions that use Tokio?
我有一个需要测试的异步函数。这个函数使用了一个mongodb::Database
对象到运行,所以我在setup()
函数中初始化连接,并使用tokio_test::block_on()
将await
表达式包裹在里面。
#[cfg(test)]
mod tests {
use mongodb::{options::ClientOptions, Client};
use tokio_test;
async fn setup() -> mongodb::Database {
tokio_test::block_on(async {
let client_uri = "mongodb://127.0.0.1:27017";
let options = ClientOptions::parse(&client_uri).await;
let client_result = Client::with_options(options.unwrap());
let client = client_result.unwrap();
client.database("my_database")
})
}
#[test]
fn test_something_async() {
// for some reason, test cannot be async
let DB = setup(); // <- 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);
}
}
只需将任何测试函数前的 #[test]
替换为 #[tokio::test]
即可。如果你使用 actix-web 你可以在测试函数
之前将 actix_rt 添加到 Cargo.toml
和 #[actix_rt::test]
中
#[tokio::test]
async fn test_something_async() {
let DB = setup(); // <- 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);
}
我有一个需要测试的异步函数。这个函数使用了一个mongodb::Database
对象到运行,所以我在setup()
函数中初始化连接,并使用tokio_test::block_on()
将await
表达式包裹在里面。
#[cfg(test)]
mod tests {
use mongodb::{options::ClientOptions, Client};
use tokio_test;
async fn setup() -> mongodb::Database {
tokio_test::block_on(async {
let client_uri = "mongodb://127.0.0.1:27017";
let options = ClientOptions::parse(&client_uri).await;
let client_result = Client::with_options(options.unwrap());
let client = client_result.unwrap();
client.database("my_database")
})
}
#[test]
fn test_something_async() {
// for some reason, test cannot be async
let DB = setup(); // <- 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);
}
}
只需将任何测试函数前的 #[test]
替换为 #[tokio::test]
即可。如果你使用 actix-web 你可以在测试函数
Cargo.toml
和 #[actix_rt::test]
中
#[tokio::test]
async fn test_something_async() {
let DB = setup(); // <- 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);
}