← Back to Rust Course | Chapter 15: Cargo, Testing & Best Practices | Lesson 6 of 8

Doc Tests

Doc tests are little code examples inside your documentation that Rust actually runs, to make sure your docs never lie.

Writing a Doc Test

A fenced code block inside a /// documentation comment above a function is automatically treated as a runnable example and executed by cargo test.

Example: Writing a Doc Test

markup
/// Adds one to the given number.
///
/// ```
/// let result = my_project::add_one(5);
/// assert_eq!(result, 6);
/// ```
pub fn add_one(n: i32) -> i32 {
    n + 1
}

fn main() {
    println!("add_one(5) = {}", add_one(5));
}

Doc Tests Keep Examples Honest

Because the example in the doc comment is actually compiled and executed, if the function's behavior ever changes in a way that breaks the example, cargo test will fail and alert you.

Example: Doc Tests Keep Examples Honest

markup
/// Doubles the given number.
///
/// ```
/// assert_eq!(my_project::double(3), 6);
/// ```
pub fn double(n: i32) -> i32 {
    n * 2
}

fn main() {
    println!("double(3) = {}", double(3));
}

Multiple Assertions in One Doc Test

A single doc test's code block can contain several statements and multiple assertions, just like an ordinary test function, to cover a few related cases at once.

Example: Multiple Assertions in One Doc Test

markup
/// Checks whether a number is positive.
///
/// ```
/// assert!(my_project::is_positive(5));
/// assert!(!my_project::is_positive(-3));
/// ```
pub fn is_positive(n: i32) -> bool {
    n > 0
}

fn main() {
    println!("is_positive(5) = {}", is_positive(5));
    println!("is_positive(-3) = {}", is_positive(-3));
}

Running Doc Tests

cargo test automatically discovers and runs every doc test alongside unit and integration tests, giving one unified command for verifying all three kinds of tests.

Example: Running Doc Tests

bash
cargo test

⚠️ Run this command in your terminal.

Common Mistakes
  1. Writing an example in a doc comment without wrapping it in triple backticks, so cargo test never recognizes it as a doc test.
  2. Letting a doc example go stale after changing the function's behavior, when cargo test would have caught the mismatch automatically.
  3. Forgetting doc tests need to actually compile and run successfully, including any assert_eq! checks placed inside them.
Chapter Summary
  • A code block inside a /// doc comment, fenced with triple backticks, is automatically compiled and run as a test.
  • Doc tests keep documentation examples accurate, since a broken example fails cargo test just like a normal test.
  • assert_eq! and similar macros can be used directly inside a doc test's code block to verify behavior.
  • Doc tests run as part of cargo test, alongside unit and integration tests.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.