Fail points for rust

7 releases (4 breaking)

0.5.1 Oct 8, 2022
0.5.0 Nov 7, 2021
0.4.0 Apr 15, 2020
0.3.0 Jul 15, 2019
0.1.0 Sep 27, 2017

#15 in Testing

Download history 76462/week @ 2023-08-15 110521/week @ 2023-08-22 114251/week @ 2023-08-29 121318/week @ 2023-09-05 134889/week @ 2023-09-12 123188/week @ 2023-09-19 129286/week @ 2023-09-26 135732/week @ 2023-10-03 141395/week @ 2023-10-10 149592/week @ 2023-10-17 157702/week @ 2023-10-24 156527/week @ 2023-10-31 151087/week @ 2023-11-07 140239/week @ 2023-11-14 71367/week @ 2023-11-21 72298/week @ 2023-11-28

457,536 downloads per month
Used in 283 crates (35 directly)


603 lines


CI Crates.io


A fail point implementation for Rust.

Fail points are code instrumentations that allow errors and other behavior to be injected dynamically at runtime, primarily for testing purposes. Fail points are flexible and can be configured to exhibit a variety of behavior, including panics, early returns, and sleeping. They can be controlled both programmatically and via the environment, and can be triggered conditionally and probabilistically.

This crate is inspired by FreeBSD's failpoints.


First, add this to your Cargo.toml:

fail = "0.5"

Now you can import the fail_point! macro from the fail crate and use it to inject dynamic failures. Fail points generation by this macro is disabled by default, and can be enabled where relevant with the failpoints Cargo feature.

As an example, here's a simple program that uses a fail point to simulate an I/O panic:

use fail::{fail_point, FailScenario};

fn do_fallible_work() {
    let _dir: Vec<_> = std::fs::read_dir(".").unwrap().collect();
    // ... do some work on the directory ...

fn main() {
    let scenario = FailScenario::setup();

Here, the program calls unwrap on the result of read_dir, a function that returns a Result. In other words, this particular program expects this call to read_dir to always succeed. And in practice it almost always will, which makes the behavior of this program when read_dir fails difficult to test. By instrumenting the program with a fail point we can pretend that read_dir failed, causing the subsequent unwrap to panic, and allowing us to observe the program's behavior under failure conditions.

When the program is run normally it just prints "done":

$ cargo run --features fail/failpoints
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/failpointtest`

But now, by setting the FAILPOINTS variable we can see what happens if the read_dir fails:

FAILPOINTS=read-dir=panic cargo run --features fail/failpoints
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/failpointtest`
thread 'main' panicked at 'failpoint read-dir panic', /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/fail-0.2.0/src/lib.rs:286:25
note: Run with `RUST_BACKTRACE=1` for a backtrace.

For further information see the API documentation.


Triggering a fail point via the HTTP API is planned but not implemented yet.