Sub topic 12
Library crates and running tests
This chapter assumes you can run shell commands, have Rust installed with rustup, and can build a binary project with cargo new and cargo run. Here you create a library crate and test it with cargo test.
Binary crates and library crates
A crate is the unit of compilation in Rust. Cargo builds two kinds from a package:
- A binary crate compiles to an executable. Its entry point is
fn main, insrc/main.rs. - A library crate compiles to code that other crates use. Its root file is
src/lib.rs, and it has nomain.
You pick the kind when you create the project. Pass --lib to get a library:
cargo new --lib adder
cd adderCargo writes Cargo.toml and src/lib.rs. It also initializes a Git repository, as it does for binary projects. The generated Cargo.toml has the same [package] and [dependencies] tables you saw earlier.
Because a library has no main, cargo run fails in this project with an error that says there is no binary target. Use cargo build or cargo check to compile it, and cargo test to exercise it.
Writing a function and a test
Replace the contents of src/lib.rs with this:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn adds_two_numbers() {
assert_eq!(add(2, 3), 5);
}
}The pieces work like this:
pubmakesaddvisible to code outside the crate.#[cfg(test)]tells the compiler to include thetestsmodule only when building for tests. It is left out of normal builds.use super::*;brings the parent module's items, such asadd, into scope.#[test]marks a function as a test.assert_eq!compares two values and panics if they differ.
Running the tests
Run:
cargo testCargo compiles the crate in test mode, runs every function marked #[test], and prints a summary. For this project the key lines look like this:
running 1 test
test tests::adds_two_numbers ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered outCargo also runs documentation tests, which are code examples in doc comments. Here there are none, so you see running 0 tests for that section.
A test passes if it returns normally and fails if it panics. Rust runs tests in parallel on separate threads, so tests should not depend on each other or on shared state.
Seeing a failure
Change the expected value to something wrong, such as assert_eq!(add(2, 3), 6);, and run cargo test again. The output names the failing test and shows both values:
assertion `left == right` failed
left: 5
right: 6The summary then reads 1 failed, and cargo test exits with a non-zero status. That status lets scripts and continuous integration systems detect the failure. Fix the value back to 5 before moving on.
Common errors
- Running
cargo runin a library. The fix is to runcargo testor add asrc/main.rs. - Forgetting
use super::*;. Without it, the test module cannot seeadd, and the compiler reports an unresolved name. - Forgetting
#[test]. The function compiles but never runs, so a broken test can look healthy. Check that the summary count matches the number of tests you wrote. - Forgetting
pub. Tests inside the same file can call private functions, but other crates cannot.
Exercise
You run cargo test in a new library project and the summary says running 0 tests, even though you wrote a test function. Name two likely causes.
Show answer
The function is missing the #[test] attribute, or it sits outside the project's compiled code. For example, it may be in a file that no mod declaration includes. A third possibility is a name filter passed to cargo test that excludes it.
Exercise
Why is the tests module marked #[cfg(test)]?
Show answer
It tells the compiler to build that module only when compiling for tests. Normal builds leave the test code out, so it adds nothing to the final library.