methodEngineering & architecture

Property-Based Testing

Property-based testing checks general rules across generated inputs, helping expose edge cases that hand-picked examples can miss.

Download Markdown (.md)

01What it means

Property-based testing expresses a behavior as a property expected to hold for an input domain, then generates many inputs to challenge it. A property may describe an invariant, an ordering relation, or agreement with a reference model. Many tools shrink failing cases into simpler counterexamples. This is testing over sampled inputs, not a mathematical proof.[1][2]

02Why it matters

Examples tend to reflect what their authors already anticipate. Generated combinations can reveal interactions among empty values, boundaries, unusual characters, and long sequences that are difficult to enumerate manually.

03When to use it

Use it for transformations, calculations, parsers, and stateful rules that have meaningful general properties. Keep explicit examples for important business cases. If the assertion merely repeats the implementation or the domain cannot be generated sensibly, added test volume provides little confidence.

04How it works

  1. State a property independently of the implementation.
  2. Define generators for realistic valid inputs and relevant invalid cases.
  3. Run enough cases within a practical time budget.
  4. Inspect and minimize counterexamples, checking both the code and the stated property.
  5. Preserve useful failures and extend generators when new risks emerge.

05A practical example

Example

A booking team checks that allocating an integer-cent discount across line items never changes its total. Generated baskets reveal a rounding case that manual examples missed. The minimized failing basket becomes an explicit regression example after the allocation logic is corrected.

06Where it breaks

A weak property can pass broken code, and restrictive generators can exclude the very cases that matter. Randomness needs reproducibility for diagnosis. Round-trip tests can miss defects shared by an encoder and decoder, so independent expectations may still be necessary.

Common mistakes

  • Assuming generated tests exhaust all possible inputs.
  • Filtering away most generated cases instead of designing the domain.
  • Writing a property that duplicates the same flawed calculation as production.

07Sources

  1. Introductionfast-check · Official documentationIntroduction
  2. Introduction to HypothesisHypothesis · Official documentationIntroduction to Hypothesis
View saved topics

Start typing to search titles, definitions, categories, and frameworks.