chapter six

6 Quick checks & 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 the question: is the code we wrote even correct? Does it do the right things? Are our algorithms producing a 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 withing Haskell. Then, QuickCheck is introduced. The chapter covers how to write properties, use the Arbitrary typeclass and how to modify testing behavior to our liking. The chapter will close by showing how to incorporate QuickCheck test suites into Stack projects.

6.1 How to test

6.1.1 Hippety, hoppety, what is a property?

6.1.2 Random does it

6.1.3 A standard for randomness

6.1.4 Random around the globe

6.1.5 A basic property test

6.1.6 Encapsulated entropy

6.2 Tailoring tests

6.2.1 The effectiveness of having no effects

6.3 Quick checks for profit

6.3.1 From bool to property

6.3.2 A generator too general

6.4 Handling arbitrary randomness

6.4.1 Yet another generator

6.4.2 Custom associative arbitrariness

6.4.3 Honey, I shrunk our test case!