Phase 3: Property-Based Testing for AI Code · 55 min · Python · Hypothesis · pytest
Hypothesis Python PBT
@given is the new @test. Strategies are the new fixtures.
Hiring signal: Engineers who can write Hypothesis property tests with @given, strategies, and shrinking demonstrate practical PBT skills that catch AI logic errors at scale.
What you will learn
- Write Hypothesis property tests using @given and strategies
- Use built-in strategies: integers, text, lists, floats, dictionaries
- Compose strategies using composite and one_of
- Apply shrinking to find minimal counterexamples
- Use the Hypothesis database to reproduce failures
The Problem
You understand PBT conceptually (Lesson 1). Now you need to write actual property tests in Python using Hypothesis. The key concepts are: @given (the decorator that turns a function into a property test), strategies (the generators that produce random inputs), and shrinking (the process that minimizes counterexamples).
The @given Decorator
@given is the core Hypothesis decorator. It takes strategies as arguments and passes randomly generated values to the test function:
from hypothesis import given
from hypothesis import strategies as st
@given(st.lists(st.integers()))
def test_sort_preserves_length(items):
result = sorted(items)
assert len(result) == len(items)
This runs test_sort_preserves_length with 200+ randomly generated lists of integers. If any input breaks the assertion, Hypothesis reports the counterexample (shrunk to minimal form).
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers Built-in Strategies, Composite Strategies, Shrinking, The Hypothesis Database, Build It — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy