Testing

#Future Reading

#Testing

# Run Tests
cargo test
cargo test should_fail
cargo test -- --nocapture # Will show staout of println!
cargo test -- --ignored # Run tests with #[ignore]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    #[should_panic]
    fn should_fail() {
        panic!("oops");
    }
}

#Integration Tests

# Run With
cargo test
# tests/test.rs
use my_crate::add;

#[test]
fn adds_two() {
    assert_eq!(add(2, 2), 4);
}

#Fuzzing

cargo install cargo-fuzz
cargo fuzz init
# Run Fuzzer
cargo fuzz run my_target
// fuzz/my_target.rs
#![no_main]
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
    // fuzz logic
    if data.len() == 3 && data[0] == b'R' && data[1] == b'S' && data[2] == b'T' {
        panic!("Found magic input!");
    }
});

#Benchmarking

cargo bench
# Cargo.toml
[dev-dependencies]
criterion = "0.5"
// benches/my_bench.rs:
use criterion::{black_box, Criterion, criterion_group, criterion_main};

fn bench_addition(c: &mut Criterion) {
    c.bench_function("addition", |b| {
        b.iter(|| {
            let x = black_box(2);
            let y = black_box(2);
            x + y
        })
    });
}

criterion_group!(benches, bench_addition);
criterion_main!(benches);