Property-based Testing in a nutshell.
Hi! Welcome to part one of my series on mental models. Click here for other parts.
Let’s say we have ourselves a sorting function called
Standardized testing would ask you to come up with some values to be tested,
whereas Property-based Testing asks you to declare the schema of the data,
and the testing framework takes care of generating the test-data for you.
The actual code is quite similar. The only difference being that in a standardized test you provide values, whereas for property-based tests you provide arbitraries* (i.e. types), and test-data is generated for you.
Standardized testing has values as first-class citizens, whereas Property-based Testing has types in its stead*.
Talk is cheap, show me the code.
Let’s take our sorting function again, and say we want to be able to test two properties:
- Whether the array that is returned is of the same size as the array that’s been passed to the function (i.e. the function argument).
- Whether all items in the returned array have a value that is greater than the value of the item preceding them (i.e.
a[n] > a[n — 1]).
And now as code:
Verdict: Add it to your toolbelt.
Property-based Testing is a relatively simple concept that compared to standardized tests may yield superior confidence at reduced costs.