This chapter covers:
- Understanding the concept of property-based testing
- Model properties by discovering appropriate data types and functions
- Fabricating test data using generators
- Minimizing test case outcomes to give meaningful feedback
- Using properties to affirm laws
- Applying syntactic sugar to improve user experience
In chapter 7 we worked through the design of a functional library for expressing parallel computations. We introduced the idea that an API should form an algebra—that is, a collection of data types, functions over these data types, and importantly, laws or properties that express relationships between these functions. We also hinted at the idea that it might be possible to somehow validate these laws automatically. Validation is an important step, as we need to know that the code we write is in conformance with the laws that we have imposed upon our program. It would be of great benefit if we could somehow automate this validation process.
This chapter will take us toward a simple but powerful library for automated property-based testing. The general idea of such a library is to decouple the specification of program behavior from the creation of test cases. The programmer focuses on specifying the behavior of a program and giving high-level constraints on the test cases. The framework then automatically generates test cases that satisfy these constraints and runs tests to validate that the program behaves as specified.