A collection of runnable PBT examples in Python and C++. Each file covers a single concept so you can jump straight to the pattern you care about.
python/ # Hypothesis + pytest
cpp/ # RapidCheck + CMake
Both directories are self-contained. No shared code, no cross-dependencies.
- Invariants: sorting preserves length and elements, map/transform preserves size
- Round-trips: JSON encode/decode, int <-> string, custom serialize/deserialize
- Idempotence: double-sort, double-abs, double-unique
- Metamorphic properties: append-then-sort length, filter bounds, double-reverse
- Error conditions: division by zero, out-of-bounds, invalid input rejection
- Custom generators: domain-specific strategies for emails, user structs, etc.
- Sneaky bugs: intentionally broken functions that look fine until PBT finds the edge cases
The sneaky bugs files are the fun part. They show why PBT matters, each function passes obvious unit tests but breaks under random input.
cd python
python -m venv venv && source venv/bin/activate
pip install -r requirements.txt
pytest -vcd cpp
cmake -B build
cmake --build build
./build/test_invariants # or any other executableRapidCheck gets pulled in automatically via CMake FetchContent on first build (needs internet).
These are supposed to fail:
# Python
pytest -v python/test_sneaky_bugs.py
# C++
./build/test_sneaky_bugsEach failure is a real bug caught by property-based testing, overflow, truncation, off-by-one, unicode mangling, that kind of thing. The counterexamples in the output show exactly what input triggers the bug.
Each test file is standalone. No conftest, no shared utils, no imports between files. Copy any file into your own project and it just works.