How to run a single doc test in Rust?

Viewed 2038

I would like to only run a particular doc test instead of running all of them. Is there a way I can do this? I know that you can pass --doc to run only doc tests but is there a flag that allows me to run just a particular one.

1 Answers

You can run doc-tests at the granularity of individual documented items.

If you have some source code like this:

/// ## Example
/// ```
/// panic!("doctest #1");
/// ```
/// ```
/// panic!("doctest #2");
/// ```
pub fn first() {}

/// ## Example
/// ```
/// panic!("doctest #3");
/// ```
pub fn second() {}

Then you can run cargo test --doc first to run doctest #1 and doctest #2, and cargo test --doc second to run doctest #3. Any tests with a path matching the string after --doc will be run.

Related