Rust "error: test failed" - A Test Panicked in CI
A cargo test run had at least one failing test. The compile succeeded - a test panicked or an assertion failed at runtime. The summary line points you to the failing tests, not a build problem.
What this error means
cargo prints per-test FAILED lines, a test result: FAILED. N passed; M failed, then error: test failed, to rerun pass ...``. This is a genuine test failure, not a transient or environment issue, and reruns the same way on the same code.
running 3 tests
test math::adds ... ok
test math::divides ... FAILED
failures:
---- math::divides stdout ----
thread 'math::divides' panicked at src/math.rs:14:5:
assertion `left == right` failed
left: 2
right: 3
test result: FAILED. 2 passed; 1 failed; 0 ignored
error: test failed, to rerun pass `--lib`Common causes
An assertion or expectation failed
A test ran and its assert!/assert_eq! (or an unexpected Result::Err/panic!) did not hold. The "left/right" diff shows the actual vs expected values.
Environment-dependent test logic
A test that depends on time, ordering, locale, or external state can fail in CI even when it passes locally - but that is still a real test issue to fix, not a flake to ignore.
How to fix it
Read the failure block and reproduce it
- Find the
---- <test> stdout ----block - it has the panic location and the assertion diff. - Re-run just that test locally:
cargo test math::divides -- --exact. - Fix the code or the test so the assertion holds; this never passes on a blind rerun.
Surface output and run single-threaded if needed
Show captured output and remove test-ordering parallelism when diagnosing flaky-looking failures.
cargo test -- --nocapture --test-threads=1How to prevent it
- Run
cargo testlocally before pushing. - Make tests deterministic - avoid hidden dependence on time, order, or locale.
- Treat CI-only failures as real bugs in the test or the code, not as flakes to retry.