BorrowCheck › Rust language core

Testing, benchmarks, profiling

Unit, integration and doc tests, should_panic, criterion, profiling, fuzzing.

The topic explains Cargo's built-in test harness and the tools for measuring performance. The questions on unit and integration tests check the requirements on a #[test] function, a test module's access to private items, what an integration test in the tests/ directory can see, where to keep shared code, how cfg(test) affects a normal build, as well as parallel execution, filtering by name, #[ignore], and the cargo test summary. The subtopic on doc tests covers how rustdoc turns an example into a separate crate, how hidden lines work, code block attributes such as compile_fail, the ? operator in an example, and why such tests are not run for a binary crate. A separate subtopic is about #[should_panic] with the expected parameter, tests returning Result, and the messages of assert! and assert_eq!. The subtopics on benchmarks, profiling and fuzzing explain why #[bench] is unavailable on the stable compiler, how to set up criterion, why black_box is needed, how to read a report and a flame graph, which build to profile, and what cargo fuzz finds.

44 questions: 21 basic, 20 intermediate, 3 advanced. 6 info pages, 8 sources.

Subtopics

  1. Unit and integration tests
  2. Doc tests
  3. #[should_panic], tests returning Result
  4. Benchmarks and criterion
  5. Profiling: perf, flamegraph
  6. Fuzzing

Previous: Macros · Next: Memory and type layout