Property Based Testing

Benedikt Maderbacher

Playlists: 'glt21' videos starting here / audio

In this talk we will explore how describe properties of your program in away that allows the computer to automatically generate hundreds of test cases for you. We will discuss this using the Hypothesis framework for Python.

Most Software Developers will be familiar with unit testing. Writing and regularly executing tests helps us to avoid bugs in our programs and increases our confidence when performing refactoring. Unit testing requires us to write down how a program is expected to behave for specific inputs.

However, there are testing techniques that go further than that, these will be the topic of this talk. In Property based testing we define abstract properties about our programs. The testing framework will then generate random input data and tries to falsify our property. If a tests fails, the framework will give us a small and often easy to read counter example.

For example, we could test a JSON library by specifying the following property: whenever we serialize a data structure as a JSON file and then parse the resulting file we end up with the same data. That is our serialization and parsing code work together correctly. Using property based testing one can write a single property instead of tens or hundreds of unit tests and still get the same level of confidence into the tested code.

In this talk you will learn how write specifications using the Hypothesis framework for Python, how to come up with good properties for your functions and how to fine tune the random generation of test cases.

While this talk uses Scala property based testing libraries are available for many major programming languages. There is the original QuickCheck for Haskell, FsCheck for F#, ScalaCheck and more.