ci / tests (macos-latest, python 3.11) (push) Canceled after 0s
ci / tests (macos-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.9) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.11) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.13) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.9) (push) Canceled after 0s
Contracts with a ladder of checks, an end-of-turn gate, retrospectives and shared lessons, time-to-delivery measurement, a library of 140 verification techniques and 20 recipes, personal data kept outside the clone, and releases with rollback. Standard library only. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
74 lines
3.1 KiB
Markdown
74 lines
3.1 KiB
Markdown
---
|
|
id: proptest
|
|
title: Property testing in Rust with proptest
|
|
answers: Does this Rust function keep its promise for every input I can generate, not just the few I thought of?
|
|
rung: 2
|
|
section: many-inputs
|
|
languages: rust
|
|
kinds: general, parsing, money, state-machine
|
|
cost: low
|
|
detect: cargo --version
|
|
ran_here: yes
|
|
verified: ran
|
|
---
|
|
|
|
## Use when
|
|
|
|
- A pure function or data structure has a law that must hold for all inputs: round trip, idempotence, agreement with a slow reference, "never panics".
|
|
- You want a failure reduced to a small input (shrinking) and replayed on every later run.
|
|
- Money, parsing or index arithmetic, where the dangerous inputs are ones nobody writes by hand.
|
|
|
|
## Do not use when
|
|
|
|
- The law cannot be stated without re-implementing the function. Use `metamorphic` or `differential-reference`.
|
|
- The property needs coverage-guided search of a deep parser. Use `cargo-fuzz`.
|
|
- A proof is affordable and a sample is not enough. See the `prove` section.
|
|
|
|
## Set up
|
|
|
|
cargo add --dev proptest
|
|
|
|
Put the test in `#[cfg(test)]` in the crate or in `tests/props.rs`. Commit the
|
|
`proptest-regressions` files that proptest writes (see `regression-seed-hygiene`).
|
|
|
|
## Minimal example
|
|
|
|
pub fn encode(x: &[u8]) -> String { x.iter().map(|b| format!("{:02x}", b)).collect() }
|
|
pub fn decode(s: &str) -> Option<Vec<u8>> {
|
|
(0..s.len()).step_by(2).map(|i| u8::from_str_radix(s.get(i..i + 2)?, 16).ok()).collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use proptest::prelude::*;
|
|
proptest! {
|
|
#![proptest_config(ProptestConfig::with_cases(1000))]
|
|
#[test]
|
|
fn round_trip(bytes in proptest::collection::vec(any::<u8>(), 0..256)) {
|
|
prop_assert_eq!(decode(&encode(&bytes)), Some(bytes));
|
|
}
|
|
}
|
|
}
|
|
|
|
## Run
|
|
|
|
cargo test round_trip
|
|
PROPTEST_CASES=10000 cargo test round_trip # more cases, same test
|
|
|
|
Passing looks like: `test tests::round_trip ... ok` and exit 0.
|
|
Failure shows `minimal failing input: bytes = [0]` after shrinking, exit 101, and
|
|
a line saying the seed was saved to a `proptest-regressions/...` file. Later runs
|
|
replay that seed first. To make a test cheap for mutation testing or a slow
|
|
host, `PROPTEST_MAX_SHRINK_ITERS=0` turns shrinking off.
|
|
|
|
## As a contract check
|
|
|
|
{"id": "prop-round-trip", "claim": "decode(encode(x)) == x for generated byte strings", "kind": "parsing", "rung": 2, "cmd": "cargo test round_trip", "expect": {"exit": 0, "stdout_contains": "1 passed"}, "tier": "deep", "timeout_s": 600}
|
|
|
|
## What a pass does not prove
|
|
|
|
- It samples. 1000 cases of small inputs say nothing about the huge or odd ones the strategy never produces: check the ranges (`0..256`) against real input sizes.
|
|
- A filter such as `prop_assume!` that rejects most cases makes a pass nearly empty. proptest aborts with "too many local rejects" when that happens; do not raise the limit to silence it.
|
|
- To check the test can fail, break the code on purpose (change `{:02x}` to `{:x}` in `encode`) and see it fail with a shrunk input, then restore it. `cargo-mutants` does this at scale.
|