Testing in Rust
In this lesson, you will learn how to write, organize, and run tests in Rust. Testing ensures your code works correctly and prevents bugs when your project grows.
Rust provides a powerful built-in testing framework that is simple, fast, and reliable.
Why Testing Is Important
Testing helps you:
- Verify correctness of code
- Catch bugs early
- Refactor safely
- Build reliable applications
Rust encourages testing by integrating it directly into the language.
Rust’s Built-In Test Framework
Rust includes testing support by default.
Tests are written as normal Rust functions annotated with #[test].
No external libraries are required.
Writing Your First Test
A basic test checks whether a condition is true.
#[test]
fn simple_test() {
assert_eq!(2 + 2, 4);
}
If the assertion fails, the test fails.
Running Tests
Use Cargo to run all tests in your project.
cargo test
Cargo compiles your code and runs every test function.
Assertions in Rust
Rust provides several assertion macros:
assert!– checks a conditionassert_eq!– checks equalityassert_ne!– checks inequality
#[test]
fn test_values() {
assert!(true);
assert_eq!(5, 5);
assert_ne!(3, 4);
}
Testing Functions
Tests are usually written to validate functions.
fn add(a: i32, b: i32) -> i32 {
a + b
}
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
}
Using #[should_panic]
Some tests are expected to fail or panic.
#[test]
#[should_panic]
fn test_panic() {
panic!("This test should panic");
}
The test passes if a panic occurs.
Testing Error Results
You can test functions that return Result.
fn divide(a: i32, b: i32) -> Result {
if b == 0 {
Err(String::from("Cannot divide by zero"))
} else {
Ok(a / b)
}
}
#[test]
fn test_divide() {
assert_eq!(divide(10, 2).unwrap(), 5);
}
Organizing Tests with Modules
Tests are often grouped inside a test module.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example() {
assert_eq!(1 + 1, 2);
}
}
Integration Tests
Integration tests live in the tests/ directory.
They test how multiple parts of your application work together.
// tests/integration_test.rs
#[test]
fn integration_test() {
assert_eq!(2 * 3, 6);
}
Running Specific Tests
You can run a single test by name.
cargo test test_add
Ignoring Tests
Some tests may be slow or optional.
#[test]
#[ignore]
fn slow_test() {
// slow code here
}
Ignored tests run only when explicitly requested.
Why Rust Testing Is Powerful
Rust testing provides:
- Fast execution
- Strong guarantees
- Clear failure messages
- Safe refactoring
📝 Practice Exercises
Exercise 1
Write a test that checks addition of two numbers.
Exercise 2
Test a function that returns a Result.
Exercise 3
Create a test that expects a panic.
Exercise 4
Run a specific test using Cargo.
✅ Practice Answers
Answer 1
assert_eq!(add(1, 2), 3);
Answer 2
assert!(divide(4, 0).is_err());
Answer 3
#[should_panic]
fn panic_test() { panic!(); }
Answer 4
cargo test test_name
What’s Next?
In the next lesson, you will learn about Documentation in Rust, including Rustdoc comments and generating project documentation.