property based testing plugin for unexpected
This plugin tries to bring generative testing (aka property testing) to the unexpected assertion library. The idea is that it should be possible to mix property based testing and normal unit-tests using the same tool chain.
If the generators you supply supports shrinking by providing a
on the generator function, unexpected-check will try to shrink the error space
as much as possible and therefore provide much more precise error cases.
I recommend using the plugin together with chance-generators as it provides a huge range of generators and supports shrinking, but it is not a requirement. You can use any function that produces a random output when called.
Let's imagine we wanted to sort arrays of numbers using this function:
Then we could write a test to ensure the following:
- the resulting array has the same size as the input array.
- the resulting array is sorted.
First we will create an assertion for checking that an array is sorted:
Then we generate the input arrays:
Finally we make the assertion:
But that assumption as actually not true as the build-in sort functions is based on converting items to strings and comparing them. So you will get the following error:
If we wanted to fix the problem, we would need to use a comparison function:
Install it with NPM or add it to your
unexpected-check.js found at the lib directory of this
this will expose the expect function under the following namespace:
Include the library with RequireJS the following way:
Notice that the chance library some we configured with the
Support for asynchronous testing by returning a promise from the subject function:
generators(default ): an array of generators used to generate the example data.
maxIterations(default 300): the number of iterations that the subject function it ran when no errors occur. You can control the default for this option by setting the environment variable
UNEXPECTED_CHECK_MAX_ITERATIONSor setting the query parameter
maxiterationsin the browser.
maxErrorIterations(default 1000): the number of iterations unexpected-check can use to find a better error when an error occurs.
maxErrors(default 50): the number of found errors before stopping the input shrinking process.
As you can see the input shrinking is worse with less iterations, but it will be a bit faster.
The source for this plugin can be found on Github.
Copyright (c) 2016 Sune Simonsen firstname.lastname@example.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.