9 Quick checks and random tests
This chapter covers
- Working with random values
- Building a small property testing framework
- Using the popular testing framework QuickCheck
- Tailoring random value generators for special test cases
- Equipping software projects with test suites
The previous chapters have dealt with writing Haskell code for various applications. Something we have neglected to ask is this: Is the code we wrote even correct? Does it do the right things? Are our algorithms producing the correct results? In software engineering, this question is often answered by testing, which is what we want to explore in this chapter. To get familiar with testing, we cover the popular testing library QuickCheck and explore property testing in the context of Haskell, learning how to formalize correctness properties for pure functional code.
This chapter will start by introducing the concept of property testing, after which we will implement our own testing framework. In the meantime, we will learn about how to work with random values within Haskell. Then, QuickCheck is introduced. The chapter covers how to write properties, use the Arbitrary
type class, and modify testing behavior to our liking. The chapter will close by showing how to incorporate QuickCheck test suites into Stack projects.