Skip to content

aedrax/PBT-Examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Property-Based Testing Examples

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.

What's in here

python/   # Hypothesis + pytest
cpp/      # RapidCheck + CMake

Both directories are self-contained. No shared code, no cross-dependencies.

Concepts covered

  • 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.

Quick start

Python

cd python
python -m venv venv && source venv/bin/activate
pip install -r requirements.txt
pytest -v

C++

cd cpp
cmake -B build
cmake --build build
./build/test_invariants   # or any other executable

RapidCheck gets pulled in automatically via CMake FetchContent on first build (needs internet).

Running the sneaky bugs

These are supposed to fail:

# Python
pytest -v python/test_sneaky_bugs.py

# C++
./build/test_sneaky_bugs

Each 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.

Structure

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.

About

Property Based Testing Examples

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors