23.7 Testing and Benchmarking (Overview)

Cargo provides first-class support for running tests and benchmarks, which are covered in detail in the next chapter.

  • cargo test: Discovers and runs tests annotated with #[test] within your src/ directory (unit tests), functions in the tests/ directory (integration tests), and code examples in documentation comments (doc tests).
  • cargo bench: Discovers and runs benchmarks annotated with #[bench]. Requires nightly Rust for the built-in harness; stable Rust typically uses external crates like criterion.