Unit Tests, Integration Tests, and Doctests
Unit Tests, Integration Tests, and Doctests
Rust has first-class built-in testing support.
Unit Tests
Unit tests live in the same source files as the code they test inside a #[cfg(test)] module:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
}
}Integration Tests
Integration tests live in the top-level tests/ directory and test the public API as an external consumer.
Documentation Tests (Doctests)
Examples written in markdown doc-comments (///) are compiled and executed during cargo test.