Sub topic 11
Dev-dependencies for tests and examples
This chapter assumes you can read a Cargo.toml, know how [dependencies] entries work, and have run cargo test at least once. It covers the [dev-dependencies] table: what it is for, where it applies, and the mistakes that come with it.
What a dev-dependency is
A dev-dependency is a crate that your project needs only while developing it. Typical uses are test helpers, benchmarking libraries, and crates used by example programs. Cargo downloads and compiles these crates only when it builds tests, examples, or benchmarks.
Users of your library never see them. If someone adds your crate as a dependency, Cargo skips your [dev-dependencies]. This keeps their build smaller and faster.
[package]
name = "my_geometry"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1", features = ["derive"], optional = true }
[dev-dependencies]
pretty_assertions = "1"
[features]
default = []
serde = ["dep:serde"]Here serde is a normal dependency, and it is optional and tied to a feature. pretty_assertions is a dev-dependency. It replaces assert_eq! with a version that prints a colored diff when two values differ.
Where dev-dependencies are available
Dev-dependencies can be used by code that Cargo builds for development only.
| Code | Can use dev-dependencies? |
|---|---|
| Unit tests in `src/` (`#[cfg(test)]`) | Yes |
| Integration tests in `tests/` | Yes |
| Doc tests | Yes |
| Examples in `examples/` | Yes |
| Benchmarks in `benches/` | Yes |
| Normal library or binary code | No |
The last row matters. If you write use pretty_assertions::assert_eq; at the top level of src/lib.rs, a normal cargo build fails, because the crate is not available there. Put such imports inside the test module instead:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn adds_two_numbers() {
assert_eq!(add(2, 3), 5);
}
}The use pretty_assertions::assert_eq; line shadows the standard macro inside this module only.
Adding one from the command line
You can add a dev-dependency without editing the file by hand:
cargo add --dev pretty_assertionsThis appends the entry to [dev-dependencies] with a compatible version.
Dev-dependencies and the build graph
The diagram shows which targets pull in which dependency tables.
Common errors
- Using a dev-dependency in shipped code. The build fails with
unresolved importoruse of undeclared crate. Move the code under#[cfg(test)], or promote the crate to[dependencies]. - Putting a test helper in
[dependencies]. Everything compiles, but every user of your library now downloads that helper. Keep test-only crates in dev-dependencies. - Expecting dev-dependencies to be transitive. If crate A has a dev-dependency on
rand, crate B that depends on A does not getrand. Declare it yourself.
Exercise
You add tempfile = "3" to [dependencies] because only your integration tests use it. What is the cost, and where should the entry go instead?
Show answer
Every crate that depends on yours will also download and compile tempfile, even though your shipped code never uses it. Move the entry to [dev-dependencies], or run cargo add --dev tempfile. Integration tests in tests/ can still use it.
Exercise
A build fails with "unresolved import pretty_assertions" after you add use pretty_assertions::assert_eq; at the top of src/lib.rs. The crate is listed under [dev-dependencies]. Why does it fail, and what is the fix?
Show answer
cargo build does not compile dev-dependencies, so normal code cannot see them. Move the use line into the #[cfg(test)] mod tests block, where the crate is available.