Sub topic 16
Running, filtering, and controlling tests
This chapter assumes you can write #[test] functions, know the difference between unit, integration, and doc tests, and can run cargo build from the command line. Here you learn how cargo test chooses which tests to run and how to change its behavior.
What cargo test does
cargo test builds your package in test mode and runs every test it finds. That includes unit tests inside src, each file in tests/, and doc tests from /// comments. Each group is run as a separate step, and Cargo prints a summary line for each one.
Arguments before -- go to Cargo. Arguments after -- go to the test binary that Cargo builds. This split explains most of the confusing errors people hit.
Filtering by name
A bare word after cargo test is a filter. Only tests whose full path contains that text will run.
cargo test
cargo test adds_two
cargo test --lib
cargo test --test area
cargo test --doccargo test adds_tworuns every test withadds_twoin its path, such astests::adds_two_numbers.--libruns only the unit tests of the library.--test arearuns only the integration test filetests/area.rs.--docruns only documentation tests.
Because the filter matches module paths too, cargo test tests:: runs every test in modules named tests. Add -- --exact to require a full-path match instead of a substring.
Showing output
The test harness captures anything a passing test prints with println!. A failing test shows its captured output. To see output from all tests as they run, disable capture:
cargo test -- --nocaptureIgnoring slow tests
Mark a test with #[ignore] to skip it by default. This suits tests that are slow or need an outside resource.
#[test]
#[ignore = "needs a running database"]
fn talks_to_database() {
assert!(true);
}Run only the ignored tests with cargo test -- --ignored. Run everything, ignored tests included, with cargo test -- --include-ignored.
Controlling parallelism
Tests run in parallel on multiple threads by default. This is fast, but tests that share a file, a port, or an environment variable can interfere with each other. Limit the thread count to run them one at a time:
cargo test -- --test-threads=1The better fix is to give each test its own resources, such as a unique temporary file. Use --test-threads=1 as a diagnostic or a last resort.
Common errors
| Symptom | Likely cause |
|---|---|
| `error: unexpected argument '--nocapture'` | The flag is before `--`, so Cargo received it. |
| `running 0 tests` | The filter matched nothing, or the test is not in the selected target. |
| Test passes alone but fails in a full run | Tests share state and race when run in parallel. |
| Integration test cannot find a function | The item is private. Integration tests see only the public API. |
Exercises
Exercise
You want to run only the tests whose names contain parse, and you want to see the println! output from passing tests. What is the command?
Show answer
cargo test parse -- --nocapture. The filter parse goes before -- as a normal argument, and --nocapture goes after -- because the test binary handles it.
Exercise
Two tests both write to a file called out.txt and sometimes fail when run together. Name a quick way to confirm parallelism is the cause, and a better long-term fix.
Show answer
Run cargo test -- --test-threads=1. If the failures disappear, the tests are racing. The better fix is to give each test its own file, for example one with a unique name in a temporary directory.