Development dependencies

Sometimes there is a need to have a dependencies for tests (examples,
benchmarks) only. Such dependencies are added to Cargo.toml in
[dev-dependencies] section. These dependencies are not propagated to other
packages which depend on this package.

One such example is using a crate that extends standard assert! macros.
File Cargo.toml:

  1. # standard crate data is left out
  2. [dev-dependencies]
  3. pretty_assertions = "0.4.0"

File src/lib.rs:

  1. // externing crate for test-only use
  2. #[cfg(test)]
  3. #[macro_use]
  4. extern crate pretty_assertions;
  5. pub fn add(a: i32, b: i32) -> i32 {
  6. a + b
  7. }
  8. #[cfg(test)]
  9. mod tests {
  10. use super::*;
  11. #[test]
  12. fn test_add() {
  13. assert_eq!(add(2, 3), 5);
  14. }
  15. }

See Also

Cargo docs on specifying dependencies.