Fuyang Liu
Fuyang Liu

Reputation: 1486

Rust doc test with async function #[tokio-test]

It seems one cannot use #[tokio-test] for test async functions in the Rust doc test?

For now I have to write an async main function and tag it with #[tokio-main] and call test_fn().await in it to let some async function run during cargo test --doc.

Is there some other better ways to allow doc tests run as if it is a normal test function, for example with #[tokio-test]? Also it would be nice if the tag #[tokio-test] can be shown in the doc, so users can copy the doc and use it as a test directly in their project. (And this probably can be done like ##[tokio-test]?)

Upvotes: 9

Views: 8282

Answers (3)

Ibraheem Ahmed
Ibraheem Ahmed

Reputation: 13538

Doc tests automatically wrap the code block in a synchronous fn main() { ... }. .awaiting requires an asynchronous runtime. You cannot .await without spawning some sort of runtime. You could spawn a regular, multi-threaded runtime for each doc test:

/// ```rust
/// #[tokio::main]
/// async fn main() {
///    let len = async { "aaaa".len() }.await;
///    assert_eq!(len, 4);
/// }
/// ```

..but that's probably not the best idea. A better way would be to use tokio_test::block_on which uses a test local runtime (similar to #[tokio::test]) to block on the provided future:

/// ```rust
/// # tokio_test::block_on(async {
/// let len = async { "aaaa".len() }.await;
/// assert_eq!(len, 4);
/// # })
/// ```

Upvotes: 11

simonamadeus
simonamadeus

Reputation: 1

I do not know enough about your special use case but maybe you want to avoid the async runtime and wrap your documentation code block like this:

/// ...
/// Example
/// ```rust,no_run // <- try using this `no_run` tag
/// ...
/// #[tokio::main]
/// async fn main() {
/// ...
/// }
/// ...
/// ```

Upvotes: -1

Raspberry1111
Raspberry1111

Reputation: 78

If you want to use asynchronous code in doctests without spawning a new runtime for each you can just wrap the whole doctest instead of just the async part in a tokio_test::block_on.

Example:

/// Asynchronously get length of "aaaa"
/// ```
/// # tokio_test::block_on(async {
///   let len = async { "aaaa".len() };
///   assert_eq!(len.await, 4)
/// # })
/// ```

Upvotes: 5

Related Questions