Course contentsShow
← Software Engineering
Lesson 606 of 1,93319. Property-Based and Fuzz TestingFree lesson

What is Property-Based Testing

Understanding how property-based testing differs from example-based testing by verifying general properties across many inputs.

What is Property-Based Testing

You'll learn: How property-based testing verifies that your code holds true across many automatically generated inputs, rather than just specific examples you write by hand.

The Core Idea

Traditional example-based testing works like this: you pick specific inputs (like add(2, 3) expecting 5) and verify the output. You're testing individual cases you thought of.

Property-based testing flips this approach. Instead of writing individual examples, you define general properties that should always be true—then the testing framework generates hundreds or thousands of random inputs to verify those properties hold.

An Analogy

Imagine testing a "sort" function:

  • Example-based: You test sort([3,1,2]) equals [1,2,3] and sort([5,5,5]) equals [5,5,5]
  • Property-based: You define properties like "the output length must equal input length" and "each element in output ≤ the next element"—then the framework tries thousands of random lists

The property-based approach catches edge cases you never thought to write, like empty lists, duplicates, negative numbers, or extremely large arrays.

Why It Matters

Example-based tests are limited by your imagination and biases. You might forget to test empty strings, negative numbers, or unusual combinations. Property-based testing explores a much wider input space automatically, often finding bugs in scenarios you'd never manually consider.

You're essentially teaching the test framework what correct behavior looks like in general terms, rather than providing a handful of specific correct answers.

Key Takeaway: Property-based testing verifies general invariants across automatically generated inputs, discovering edge cases beyond what you'd manually write in example-based tests.