Module proptest

Source
Expand description

Property-based testing framework for Rust.


proptest is a property-based testing framework inspired by QuickCheck and Hypothesis. Instead of writing tests with specific hardcoded inputs, proptest generates many random test inputs to verify that properties of your code hold across a wide range of cases.

The crate automatically generates test data using Strategy types, runs tests with the proptest! macro, and when tests fail it shrinks the failing input to find the minimal test case that reproduces the failure. This helps discover edge cases and boundary conditions that might not be obvious from example-based testing.

Key features include strategies for generating primitives, collections, and custom types, the prop_assert! family of assertion macros, and automatic test case shrinking to minimal failing examples.

Proptest integrates with Rust’s standard test harness and works alongside regular unit tests. It’s particularly useful for testing functions with complex input spaces, validating invariants, and ensuring code behaves correctly across edge cases.

§Examples

Basic property test verifying that reversing a vector twice returns the original:

use proptest::prelude::*;

proptest! {
    #[test]
    fn test_double_reverse(v: Vec<i32>) {
        let reversed_twice: Vec<_> = v.iter().cloned().rev().rev().collect();
        prop_assert_eq!(v, reversed_twice);
    }
}

Testing that addition is commutative:

use proptest::prelude::*;

proptest! {
    #[test]
    fn test_addition_commutative(a: i32, b: i32) {
        let sum1 = a.saturating_add(b);
        let sum2 = b.saturating_add(a);
        prop_assert_eq!(sum1, sum2);
    }
}

Using custom strategies to generate constrained data:

use proptest::prelude::*;

proptest! {
    #[test]
    fn test_positive_division(a in 1..1000i32, b in 1..1000i32) {
        let result = a / b;
        prop_assert!(result >= 0);
        prop_assert!(result <= a);
    }
}

Modules§

arbitrary
Defines the Arbitrary trait and related free functions and type aliases.
array
Support for strategies producing fixed-length arrays.
bits
Strategies for working with bit sets.
bool
Strategies for generating bool values.
char
Strategies for generating char values.
collection
Strategies for generating std::collections of values.
num
Strategies to generate numeric values (as opposed to integers used as bit fields).
option
Strategies for generating std::Option values.
path
Strategies for generating [PathBuf] and related path types.
prelude
Re-exports the most commonly-needed APIs of proptest.
result
Strategies for combining delegate strategies into std::Results.
sample
Strategies for generating values by taking samples of collections.
strategy
Defines the core traits used by Proptest.
string
Strategies for generating strings and byte strings from regular expressions.
test_runner
State and functions for running proptest tests.
tuple
Support for combining strategies into tuples.

Macros§

prop_assert
Similar to assert! from std, but returns a test failure instead of panicking if the condition fails.
prop_assert_eq
Similar to assert_eq! from std, but returns a test failure instead of panicking if the condition fails.
prop_assert_ne
Similar to assert_ne! from std, but returns a test failure instead of panicking if the condition fails.
prop_assume
Rejects the test input if assumptions are not met.
prop_compose
Convenience to define functions which produce new strategies.
prop_oneof
Produce a strategy which picks one of the listed choices.
proptest
Easily define proptest tests.