Unit Tests
test attribute.
Testing in Rust
Rust has built-in testing — no external framework needed. Tests are ordinary functions annotated with #[test], run by cargo test.
Your First Test
A test function takes no arguments and uses assertions. If it returns normally, it passes; if it panics, it fails.
fn add(a: i32, b: i32) -> i32 {
a + b
}
#[test]
fn it_adds() {
assert_eq!(add(2, 3), 5);
}
fn main() {
println!("{}", add(2, 3));
}All lessons in this course
- Unit Tests
- Integration Tests
- Doc Comments
- Doc Tests