Use proptest to define property-based tests that automatically generate random inputs to verify your code's logic holds true across all scenarios.
Use proptest by adding it as a dev-dependency, defining a test function with the #[test] attribute, and running a strategy via prop_assert! or prop_assert_eq! to verify properties across random inputs.
#[cfg(test)]
mod tests {
use proptest::prelude::*;
#[test]
fn test_addition_commutative() {
proptest!(|(a in any::<i32>(), b in any::<i32>())| {
prop_assert_eq!(a + b, b + a);
});
}
}
Run the tests with cargo test.
Proptest is a tool that automatically generates random data to test your code against many different scenarios at once. Instead of writing tests for specific numbers, you define rules (like 'adding A and B should equal adding B and A') and let the tool try thousands of random combinations to find bugs you might miss. It works like a stress test that checks if your logic holds up under any possible input.