Module proptest
Property-based testing framework for Rust.
- Crate
::proptest. - docs.rs
- crates.io
- GitHub
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!
Testing that addition is commutative:
use *;
proptest!
Using custom strategies to generate constrained data:
use *;
proptest!
Modules
-
arbitrary
Defines the
Arbitrarytrait 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
boolvalues. -
char
Strategies for generating
charvalues. -
collection
Strategies for generating
std::collectionsof values. - num Strategies to generate numeric values (as opposed to integers used as bit fields).
-
option
Strategies for generating
std::Optionvalues. -
path
Strategies for generating
PathBufand 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
proptesttests.