The birth of unexpected-check
Property based testing
-what is it all about?
Property based testings makes statements about a piece of code while being exercised with data taken from a given input space.
Problems that are well suited for property based testing
Functions that have an inverse function
- reverse
- encode/decode
- add/remove
Imagine we wanted to test the encodeURIComponent function.
Example-based testing
Property-based testing
decodeURIComponent(encodeURIComponent(s)) = s
Let's start out by generating some strings:
Functions where it is easier to check the result than computing it
- sorting
- searching
- most transformations
Testing a sorting function
isSorted(sort(a)) = true
Let's make an assertion for checking
that arrays are sorted ascending:
Quicksort sorts arrays of strings correctly
...and integer arrays:
Let's try the same with the build-in sorting function:
And it fails!
We get the following error:
Objects that maintains an invariant
- sets
- queues
- balanced search trees
Testing a queue
While adding and removing items, the items should always be retrieved in the same order they where inserted.
Let's generate some operations:
This will generate arrays with the following structure:
Reuse generated operations:
Input shrinking
The assertion finds an error in the first iteration and shrinks it to the optimal output in 14 iterations. Here are the last iterations:
Shrinking a number:
The shrunken values will converge towards zero.
Shrinking an array:
The shrunken arrays will converge towards the smallest possible array.
What will the future bring?
Improved shrinking
Shrinking nested generators
The sorting failure we saw earlier: [-45,-95]
should have been: [10,2]
.
Done
More generators
- json-schema-faker
- immutable-js generators
- Generator for React prop types
Support asynchronous assertions
Useful for testing for race conditions with random executions plans.
Done