24.11 Summary
Testing remains crucial—even in a language with strong safety guarantees like Rust. In this chapter, we covered:
- What testing is and why it is essential for correctness.
- Types of tests: unit tests (within the same module), integration tests (
tests/
directory), and documentation tests (within doc comments). - Creating and running tests using
#[test]
andcargo test
. - Assertion macros (
assert!
,assert_eq!
, andassert_ne!
). - Error handling with
#[should_panic]
, returningResult<T, E>
from tests, and handling panic messages. - Filtering tests by name, viewing or ignoring output, using
#[ignore]
, and controlling concurrency. - Benchmarking via the nightly harness or using crates such as
criterion
anddivan
.
By combining thorough testing with Rust’s compile-time safety guarantees, you can confidently develop robust, maintainable, and high-performance systems.