Property-Based Testing with Hypothesis
Find edge cases with round trips, reference results and invariants.

What you will build
Three property tests pass. demonstrate_bug.py intentionally fails and is run separately; it illustrates why joining text with a comma is not a general-purpose serialisation format.
This workbook is for readers who can run a Python script. It includes a small local example, explanations, exercises and worked answers. The package was checked on Windows with Python 3.13.17. Pinned package versions and setup instructions are included; they are not a claim to be the latest releases.
Read a sample
The following two sections are taken from the practical download edition.
Describe behaviour before choosing examples
An example test checks one input and one expected result. A property test states a relationship that should hold for many inputs. Both belong in a test suite. Here, our first relationship is a round trip: decoding an encoded JSON-compatible value should recover the original value.
Read codec.py before running the tests. encode uses JSON, escapes non-ASCII characters and refuses non-standard floating-point values. decode reverses the process. The generated input domain contains null, booleans, integers, text, lists and string-keyed dictionaries. It excludes floats, tuples, dates and custom objects because their representation would need additional decisions.
A strategy is an input specification, not a claim that every imaginable input was tested. If your production application accepts more types, add them with a clear equality rule. NaN is a useful example: it is not equal to itself, so a naive round-trip equality property needs special handling.
Build nested values without hiding the edges
The json_scalars strategy combines simple values. st.recursive then builds bounded lists and dictionaries from those leaves. max_leaves limits how large nested examples become, while max_size limits individual collections. These controls keep the test practical without restricting it to one hand-written shape.
The explicit @example adds a readable nested case alongside generated examples. Keep important regressions as explicit examples even when a strategy could generate them. This makes the purpose of the test easier to see and preserves a case across changes to the input strategy.
Avoid filtering away inputs just because they break the current implementation. A comma inside a string may be exactly the input needed to reveal a delimiter bug. First decide whether the input is valid for the promised interface; then fix the implementation or narrow the documented contract.
Inside the complete guide
- Describe behaviour before choosing examples
- Build nested values without hiding the edges
- Watch a deliberately broken property fail
- Check more than one property
- Exercises and worked answers
Files in your download
The 6-page PDF includes the walkthrough and complete source listing. The examples folder contains:
codec.pytest_properties.pydemonstrate_bug.pyREADME.txtrequirements.txt
Scope and source material
This is a focused learning project with synthetic example data. It does not include hosted services or production deployment. This revised edition is based on previously published articles and uses replacement runnable examples; it is not a verbatim reprint.
Get the guide on Payhip — £4.99