Property-Based Testing
When to use: parsers, serializers, data transformers, encoders/decoders, validators — anything where "for all valid inputs X, property Y holds."
The principle: Instead of testing specific examples, describe properties of the output and let the framework generate hundreds of random inputs to try to falsify them.
Platform tools:
| Platform | Library | Install |
|---|---|---|
| Python | Hypothesis | pip install hypothesis |
| TypeScript/JS | fast-check | npm install fast-check |
| Swift | @Test(arguments:) (parameterized) |
Built into swift-testing |
| .NET | FsCheck | dotnet add package FsCheck |
| Kotlin/JVM | jqwik | Gradle/Maven dependency |
At least one property test MUST be written per data transformation function. Examples:
encode(decode(x)) == x(round-trip)sort(xs).length == xs.length(preservation)parse(serialize(obj)).fields == obj.fields(fidelity)
from hypothesis import given
import hypothesis.strategies as st
@given(st.text())
def test_encode_decode_roundtrip(s):
assert decode(encode(s)) == s