Skip to content
Latchkey

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.

cargo test
running 3 tests
test math::adds ... ok
test math::divides ... FAILED

failures:
    math::divides
test result: FAILED. 2 passed; 1 failed; 0 ignored

Common 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

  1. Copy the test path from the failures: list.
  2. Run just that test with captured output: cargo test math::divides -- --nocapture.
  3. Fix the assertion or the code it exercises, then re-run the single test.
Terminal
cargo test math::divides -- --nocapture

Stabilize a flaky test

  1. Remove dependence on shared global state, real time, or network in the test.
  2. Force deterministic ordering or seed any randomness used by the test.
  3. 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 --locked so a dependency change cannot silently alter behavior.
  • Quarantine known-flaky tests rather than letting them randomly red the pipeline.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →