On this page
  1. Testing, Clippy & Rustdoc
  2. Contents
  3. Unit Tests & Doc-Tests
  4. Standard Quality Commands

Testing, Clippy & Rustdoc

Prev: Cargo, Workspaces & Profiles | Up: cargo_and_tooling | Next: tutorials
Rel: Testing & Maintainability
Ref: The Rust Book — Writing Automated Tests
Tags: #public #prog #rust #tooling #testing


Contents


Unit Tests & Doc-Tests

Place unit tests in a #[cfg(test)] mod tests block inside the same file (compiled out of release binaries), and executable examples inside /// doc comments:

/// Doubles an integer.
///
/// ```
/// assert_eq!(my_crate::double(21), 42);
/// ```
pub fn double(x: i32) -> i32 {
    x * 2
}

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

    #[test]
    fn doubles_negative() {
        assert_eq!(double(-3), -6);
    }
}

Standard Quality Commands

cargo test                          # Unit, integration, and doc tests
cargo clippy --all-targets -- -D warnings
cargo fmt --check
cargo doc --no-deps --open

Linked from

Related notes