Rust "cargo test failed" - Find the Failing Test in CI
cargo test compiled fine but at least one test failed or panicked. The signal is the test result: FAILED line plus the failures: list -- that names the test you need to look at.
What this error means
Output shows test result: FAILED. N passed; M failed and a failures: section listing each failing test with its assertion or panic message. A test that fails only sometimes is flaky -- ordering, timing, or shared state -- not a clean break.
running 3 tests
test math::adds ... ok
test math::divides ... FAILED
failures:
math::divides
test result: FAILED. 2 passed; 1 failed; 0 ignoredCommon causes
A genuine assertion failure or panic
The code under test produced a wrong value, or a test hit an unwrap() on a None/Err. The assertion left == right`` or panic message states exactly what diverged.
A flaky test
A test that depends on wall-clock timing, network, filesystem state, or run order can pass locally and fail in CI. Re-running it changes the result -- a clear flakiness signal.
How to fix it
Run the one failing test with output
- Copy the test path from the
failures:list. - Run just that test with captured output:
cargo test math::divides -- --nocapture. - Fix the assertion or the code it exercises, then re-run the single test.
cargo test math::divides -- --nocaptureStabilize a flaky test
- Remove dependence on shared global state, real time, or network in the test.
- Force deterministic ordering or seed any randomness used by the test.
- If it touches external services, mock them or gate the test behind a feature.
How to prevent it
- Keep tests hermetic -- no shared mutable state, no real network, no wall-clock sleeps.
- Run
cargo test --lockedso a dependency change cannot silently alter behavior. - Quarantine known-flaky tests rather than letting them randomly red the pipeline.