I have been writing a lot of Rust lately and as a consequence I have had to get a lot better at writing unit tests. As if testing along weren’t tricky enough, almost everything I am writing takes advantage of async/await and is running on top of the async-std runtime.

Testing async functions with async-std is actually so easy it’s no wonder nothing showed up in my first search engine queries! Rather than using #[test] use the #[async_std::test] notation and convert the test function to an async function, for example:

async fn simple_method() -> boolean {
    true
}

#[cfg(test)]
mod tests {
    use super::*;

    #[async_std::test]
    async fn test_simple_case() {
        let result = simple_method().await;
        assert!(result);
    }
}

That was easy™

Another option which also worked out just fine was to utilize smol as in dev-dependencies to run something like:

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_case() {
        let result = smol::run(simple_method());
        assert!(result);
    }
}