Sub topic 14
Integration tests against the public API
This chapter assumes you know how to write unit tests with #[cfg(test)], how pub controls visibility, and the difference between a library crate and a binary crate. Integration tests check your crate the way a user would, through its public API only.
How the tests directory works
Cargo treats every .rs file directly inside tests/ (next to src/) as its own crate. Each one is compiled separately and linked against your library. You don't need #[cfg(test)], because Cargo only builds these files for cargo test.
// tests/area.rs
use my_geometry::add;
#[test]
fn add_works_through_public_api() {
assert_eq!(add(10, 20), 30);
}The test imports my_geometry by its package name, like any external user. Anything not marked pub, or not reachable through pub use, is invisible here.
Unit tests versus integration tests
| Aspect | Unit tests | Integration tests |
|---|---|---|
| Location | `#[cfg(test)] mod tests` in `src/` | Files in `tests/` |
| Crate | Same crate as the code | Separate crate per file |
| Private items | Visible through `use super::*` | Not visible |
| Best for | Internal logic and edge cases | Public behavior and how parts work together |
Requirements and limits
Integration tests only work for a library target. A package that has only src/main.rs exposes nothing to import. This is why the usual pattern is a thin main.rs that calls into src/lib.rs, so the logic can be tested.
Because each file is its own crate, two test files cannot see each other's helpers. To share setup code, put it in a subdirectory:
// tests/common/mod.rs
pub fn setup_value() -> i32 {
10
}// tests/with_helper.rs
mod common;
#[test]
fn uses_shared_helper() {
assert_eq!(common::setup_value(), 10);
}Use tests/common/mod.rs rather than tests/common.rs. Cargo compiles every top-level file in tests/ as a test crate, so common.rs would show up in the test output as a test file with zero tests. Files in subdirectories are not treated that way.
Running integration tests
Select a single file with --test, using the file name without .rs:
cargo test --test areaA plain cargo test runs unit tests first, then each integration file, then doc tests. If a unit test fails, Cargo stops before running the later groups. Add --no-fail-fast to run everything regardless.
Common errors
unresolved import my_geometry: the package has nosrc/lib.rs, or the crate name inusedoesn't match the package name. Hyphens in the package name become underscores.function ... is private: the test reached for an item that isn't public. Either export it deliberately or test it in a unit test instead.- A
commonentry appearing in test output: the helper was placed intests/common.rsinstead oftests/common/mod.rs.
Exercise
You have a package with only src/main.rs containing a function parse_config. You add tests/config.rs with use my_app::parse_config;. It fails to compile. Why, and what is the usual fix?
Show answer
Integration tests link against a library crate, and a binary-only package has none, so there is nothing to import. Move the logic into src/lib.rs, mark parse_config as pub, and have main.rs call it through the library.
Exercise
Why should shared test helpers go in tests/common/mod.rs instead of tests/common.rs?
Show answer
Cargo compiles every top-level .rs file in tests/ as a separate test crate. tests/common.rs would run as a test file with no tests and clutter the output. A file in a subdirectory is not compiled that way, so it is only used when another test file declares mod common;.