Property-Based Testing with StreamData
Go beyond example-based tests by generating hundreds of random inputs with StreamData to find edge cases ExUnit examples miss.
Property-Based Testing with StreamData is a free Elixir & Phoenix: Scalable Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elixir & Phoenix: Scalable Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Property-Based Testing?
Traditional tests check specific examples. Property-based testing describes a rule that should hold for all inputs, then generates many random inputs to try to break it.
It excels at finding edge cases you never thought to write by hand.
Adding StreamData
StreamData is the Elixir library for generating test data. Add it to your dependencies.
defp deps do
[{:stream_data, "~> 0.6", only: :test}]
endImporting the Helpers
Use ExUnitProperties in your test module to unlock the check all and property macros.
defmodule MyApp.MathTest do
use ExUnit.Case
use ExUnitProperties
endWriting a Property
The property macro replaces test. Inside, check all draws values from a generator and runs the body for each.
property "reversing twice returns the original list" do
check all list <- list_of(integer()) do
assert Enum.reverse(Enum.reverse(list)) == list
end
endBuilt-in Generators
StreamData provides generators for common types:
integer(),float(),boolean()string(:alphanumeric)list_of/1,map_of/2
check all name <- string(:alphanumeric), age <- integer(0..120) do
assert build_user(name, age).age >= 0
endCombining Generators
Use tuple/1 or bind multiple generators in one check all to produce structured data.
check all {a, b} <- tuple({integer(), integer()}) do
assert a + b == b + a
endTransforming with map
StreamData.map/2 reshapes generated values, e.g. building positive integers from any integer.
positive = StreamData.map(integer(), &abs/1)
check all n <- positive do
assert n >= 0
endFiltering Inputs
filter/2 discards values that do not match a condition. Use it sparingly — heavy filtering slows generation.
non_zero = StreamData.filter(integer(), &(&1 != 0))
check all d <- non_zero do
assert div(d, d) == 1
endShrinking Failures
When a property fails, StreamData shrinks the input — repeatedly simplifying it — to report the smallest failing example. This makes debugging far easier than a random huge value.
Choosing Good Properties
Effective properties describe invariants rather than restating the implementation:
- Round-trip: encode then decode returns the original
- Idempotency: applying twice equals applying once
- Invariants: a sorted list is always ordered
When to Use It
Property tests complement, not replace, example tests. Reach for them on pure functions, parsers, serializers, and data transformations where many inputs share one rule.
Quick Check
Test your property-based testing knowledge.
Recap
You learned property-based testing with StreamData:
- Describe rules that hold for all inputs
- Use
property+check allwith generators - Combine, map, and filter generators
- Shrinking reports the minimal failing case
- Best for invariants, round-trips, and idempotency
It uncovers edge cases example tests miss.
Frequently asked questions
Is the “Property-Based Testing with StreamData” lesson free?
Yes — the full text of “Property-Based Testing with StreamData” is free to read here on the web, and the Elixir & Phoenix: Scalable Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elixir & Phoenix: Scalable Backend Development course, upgrade to CoddyKit PRO.
What will I learn in “Property-Based Testing with StreamData”?
Go beyond example-based tests by generating hundreds of random inputs with StreamData to find edge cases ExUnit examples miss. You practise Elixir & Phoenix: Scalable Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Elixir & Phoenix: Scalable Backend Development?
No prior experience is required. Elixir & Phoenix: Scalable Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Property-Based Testing with StreamData” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Elixir & Phoenix: Scalable Backend Development lesson?
Yes. Every Elixir & Phoenix: Scalable Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Unit Testing with ExUnit
- Integration Testing Phoenix Applications
- Mocking, Stubs, and Test Data
- Property-Based Testing with StreamData