0PricingLogin
Learn Rust Coding · Lesson

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

  1. Unit Tests
  2. Integration Tests
  3. Doc Comments
  4. Doc Tests
← Back to Learn Rust Coding