Sub topic 7
Doc tests: examples that run as tests
This chapter assumes you can write a library crate with Cargo, use /// comments and Markdown, and write tests with assert_eq!.
What a doc test is
A doc test is a code block inside a doc comment that cargo test --doc compiles and runs. Examples in prose go stale when the code changes. A doc test fails when its example stops working, so the documentation stays correct.
Rustdoc treats every fenced code block in a doc comment as Rust unless you mark it otherwise. This chapter uses ~~~ fences inside the comments so the samples fit in this page. Rustdoc accepts both ~~~ and triple backticks.
/// Adds two numbers.
///
/// # Examples
///
/// ~~~
/// use mycrate::add;
///
/// assert_eq!(add(2, 3), 5);
/// ~~~
pub fn add(a: i32, b: i32) -> i32 {
a + b
}The crate is assumed to be named mycrate. Run the doc tests with:
cargo test --docEach example appears in the output as its own test, named after the file and line of the block:
running 1 test
test src/lib.rs - add (line 5) ... okA plain cargo test also runs doc tests, after the unit and integration tests.
How rustdoc runs an example
Rustdoc does not run your examples inside your crate. It handles each block like this:
- It extracts the block from the comment.
- It wraps the code in
fn main() { ... }unless the block already definesmain. - It compiles the result as a separate program that links your crate as an external dependency.
- It runs that program. The test passes if the program exits normally.
Two consequences follow from this model:
- The example sees only your public API, exactly as a user of the crate does. Private functions are not reachable, even from a doc comment on that same function.
- Nothing is imported for you. Write
use mycrate::add;or callmycrate::addby its full path.
Because each example is compiled separately, many doc tests make cargo test --doc slower than unit tests. Rustdoc merges compatible examples where it can, but keep examples short and few.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `unresolved import` or `cannot find function` | The example is missing a `use`, or the item is private | Add the import, or make the item `pub` |
| `the ? operator can only be used in a function that returns Result` | Rustdoc's generated `main` returns `()` | End the example with an explicit `Ok::<(), ErrorType>(())`, covered in a later chapter |
| Test passes but the example is wrong | A block was marked `ignore`, or it has no assertion | Remove `ignore`, and add an `assert!` or `assert_eq!` |
| Example fails after a refactor | The public API changed | Update the example. This is the failure doc tests exist to catch |
A doc test that only calls a function proves little. End it with assert_eq! or assert! so a change in behavior fails the test.
Running a subset
You can pass a filter to run only some doc tests:
cargo test --doc addThis runs the doc tests whose names contain add.
Exercise
A private helper function fn clamp(x: i32) -> i32 has a doc comment with an example that calls mycrate::clamp(5). Why does cargo test --doc fail on it, and what are two ways to fix it?
Show answer
Rustdoc compiles the example as a separate program that links your crate, so it can only use the public API, and clamp is private. You can make clamp public with pub, or remove the code block from the doc comment (or turn it into a plain prose description).