Rust
Beginner
1 min read
Testing, Workspaces, and cargo publish
Example
// src/lib.rs — library with unit tests and doc tests
/// Adds two numbers together.
///
/// # Examples
///
/// ```
/// assert_eq!(my_lib::add(2, 3), 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn divide(a: f64, b: f64) -> Option<f64> {
if b == 0.0 { None } else { Some(a / b) }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add_positive() {
assert_eq!(add(2, 3), 5);
}
#[test]
fn test_add_negative() {
assert_eq!(add(-1, -2), -3);
}
#[test]
fn test_divide_normal() {
assert!((divide(10.0, 4.0).unwrap() - 2.5).abs() < 1e-10);
}
#[test]
fn test_divide_by_zero() {
assert_eq!(divide(1.0, 0.0), None);
}
#[test]
#[should_panic(expected = "intentional")]
fn test_panic() {
panic!("intentional panic for demonstration");
}
}
// tests/integration_test.rs (separate file in tests/ directory)
// use my_lib;
// #[test]
// fn integration_add() {
// assert_eq!(my_lib::add(10, 20), 30);
// }
// Run all tests: cargo test
// Run a specific test: cargo test test_add_positive
// Run doc tests only: cargo test --doc
// Run integration tests only: cargo test --test integration_test
Related Resources
This is the last lesson in this section.
Create a free account to earn a certificate