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.

9.1 How to test

9.1.1 Property testing

9.1.2 Generating random values

9.1.3 Random and Uniform

9.1.4 Using a global random value generator

9.1.5 A basic property test

9.1.6 Defining postconditions for random values

9.2 Randomized testing

9.2.1 The benefit of referential transparency

9.3 The QuickCheck testing framework

9.3.1 Using Property in QuickCheck

9.4 Generating random values for testing

9.4.1 The random generator Genrandom value function

9.4.2 Example: AssocMap

9.4.3 Shrinking test cases

9.5 Practical usage of property testing

9.5.1 Verbosity and coverage reports

9.5.2 Modifying a test’s parameters

9.5.3 Constructing test suites

Summary