Skip to content
Latchkey

cargo "test failed, to rerun pass --lib" in CI

A test assertion failed or a test panicked. cargo prints the failing test names, a FAILED summary, and "error: test failed, to rerun pass --lib" so you can re-run just that binary. The real cause is in the panic output above.

What this error means

cargo test ends with a "test result: FAILED" line, the names of failing tests, and "error: test failed, to rerun pass --lib" (or --bin X, --test Y).

cargo
running 2 tests
test math::tests::it_adds ... FAILED

failures:
---- math::tests::it_adds stdout ----
thread 'math::tests::it_adds' panicked at src/math.rs:21:9:
assertion `left == right` failed
  left: 4
 right: 5

error: test failed, to rerun pass `--lib`

Common causes

A failing assertion or panic in a test

The test body asserted something false or hit a panic; the captured stdout shows the assertion and source location.

A test that depends on environment or ordering

Tests that read the clock, locale, network, or shared state can pass locally but fail under CI's different environment or parallelism.

How to fix it

Rerun the failing target and read the panic

  1. Use the exact rerun hint cargo printed (--lib, --bin, --test).
  2. Read the panic message and source span in the captured stdout.
  3. Fix the assertion or the code it exercises.
Terminal
cargo test --lib math::tests::it_adds -- --exact --nocapture

Remove environment and ordering coupling

Isolate per-test state, inject time and locale, and avoid relying on test execution order so CI and local agree.

Terminal
RUST_TEST_THREADS=1 cargo test

How to prevent it

  • Run the full test suite locally before pushing.
  • Isolate state and inject time/locale so tests are deterministic.
  • Use the printed rerun hint to focus on the failing target.

Related guides

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