How to Write Property-Based Tests in Rust (proptest, quickcheck)

Use the proptest crate to define tests that automatically generate random inputs to verify properties hold true for all cases.

Use the proptest crate to define a test function that takes arbitrary inputs and asserts a property holds for all generated values. Add proptest to your Cargo.toml dependencies, import the proptest macro, and define a test function that takes arguments matching the property you want to verify.

use proptest::prelude::*;

proptest! {
    #[test]
    fn addition_is_commutative(a: i32, b: i32) {
        prop_assert_eq!(a + b, b + a);
    }
}

Run the test with cargo test to automatically generate thousands of random inputs and verify the property.