Sub topic 11
Fewer test binaries, faster test builds
This chapter assumes you can run cargo test, know the difference between unit and integration tests, and have read the earlier chapters on linking and workspaces.
Why every test file costs you
Cargo treats each file directly under tests/ as its own crate. Each one is compiled separately and linked into its own executable. A project with 30 files in tests/ therefore produces 30 test binaries, and each one repeats the link step and re-links your library and its dependencies.
Linking is often the slowest part of a test build, so the number of binaries matters more than the number of tests. Many small integration test files make cargo test slow even when the tests themselves run in milliseconds.
Combine integration tests into one binary
Cargo only treats files at the top level of tests/ as crate roots. Files inside subdirectories are ignored unless something declares them as modules. You can use this to build a single binary.
Use this layout:
tests/
integration.rs
integration/
parser.rs
storage.rs
cli.rsThe file tests/integration.rs is the only crate root:
mod parser;
mod storage;
mod cli;Because integration.rs is at the top level, Cargo looks for its modules in tests/integration/. Three test files now share one compile and one link.
Trade-offs of merging
Merging has costs, so decide based on your project:
- Tests in one binary share a process. A test that sets global state, such as environment variables, can affect its neighbors. Separate binaries gave you that isolation for free.
- A change to any test module recompiles the whole merged crate. This is usually still cheaper than relinking many binaries.
- Test names now include the module path, for example
parser::handles_empty_input. Filters likecargo test parser::still work. - Shared helpers become simpler. You can put them in one
commonmodule instead of atests/common/mod.rsthat each file includes separately.
Keep a separate top-level file when a test needs its own process, such as one that depends on a custom main or on global initialization.
Skip what you do not need
Cargo builds more targets than you may expect. These settings cut work from the test build.
Turn off the unit-test harness for targets that have no tests. This avoids building a test binary for them:
# Cargo.toml
[lib]
test = false
doctest = falseOnly do this if the library really has no unit tests or doc tests. Doc tests are especially costly, because rustdoc compiles every code example as a separate program. A crate with many examples can spend more time in doc tests than in everything else. You can skip them when iterating:
# Run only unit and integration tests
cargo test --lib --tests
# Run one integration binary
cargo test --test integrationRun cargo test --doc separately, for example in CI.
Use a workspace-aware layout
In a workspace, cargo test builds the tests of every member. Put slow, heavy tests in the crates that own the code they test, and run only the affected crate while you work:
cargo test -p storageUnchanged crates are not rebuilt, so this fits well with the workspace split from the earlier chapter.
Common errors
- Adding a new file to
tests/integration/and finding that it does not run. The file must be declared withmod name;inintegration.rs. - Putting a
mainfunction or#[test]code intests/integration/mod.rsand expecting it to be a separate crate. Files in subdirectories are modules, not crates. - Disabling
doctestand later wondering why examples in the documentation stopped being checked.
Exercise
Your tests/ directory holds a.rs, b.rs and c.rs, and cargo test is slow because of linking. How many test binaries does Cargo build for them, and how do you reduce that to one?
Show answer
Cargo builds three binaries, one per top-level file. Move the files to tests/integration/a.rs, b.rs and c.rs, then create tests/integration.rs containing mod a; mod b; mod c;. Only integration.rs is a crate root, so Cargo builds and links one binary.