Tdd Property Testing

Using generative testing to explore large input spaces and find obscure bugs.

jcorpac 6726a1f 1.2 KB Updated

File contents

Property-Based Testing

Instead of writing individual test cases with fixed inputs, you define properties (invariants) that should always be true for any valid input.

How it Works

  1. Define a strategy for generating random data (e.g., "any list of integers").
  2. Define a property (e.g., "sorting a list doesn't change its length").
  3. The framework generates hundreds of inputs to find a counter-example.
  4. If it finds one, it shrinks the input to the simplest possible case that fails.

Tools

Example (Python with Hypothesis)

from hypothesis import given, strategies as st

def add(a, b):
    return a + b

@given(st.integers(), st.integers())
def test_add_commutative(a, b):
    assert add(a, b) == add(b, a)

When to Use

  • Mathematical functions.
  • Encoders/Decoders.
  • Data transformation pipelines.
  • Sorting or searching algorithms.

jcorpac/ai-skills-library/tree/main/tdd/tdd-property-testing commit 6726a1fea3

Frequently asked questions

npx skillmds@latest add jcorpac/tdd-property-testing