Property Based Testing

Property based testing is an extremely powerful style of writing tests that originated with the Haskell QuickCheck library.

In traditional unit tests, the programmer provides some specific data and writes tests that verify that the code under test gives the right results.

With property based testing, you specify properties that your functions must have, and the testing framework produces dozens, or hundreds, or even millions of examples of data to test those properties with. Properties can be almost anything ("the output is valid JSON", "the result is not null", "an exception is not thrown"). This is closely related to the better known concept of fuzzing. David R. MacIver, who maintains the python property based testing library Hypothesis (and who provides almost every link in this post) wrote a more involved definition of property based testing, but it's not necessary to understand the idea or start employing property based testing.

MacIver also collected a list of property based testing libraries in different languages and evaluated several of them. Most popular languages have at least one implementation.

One challenge of property based testing is coming up with good properties to test, so there's a section below on thinking of good properties.

Defining Properties

Getting Started With Hypothesis

The simplest property is: when I run my code, does it produce unexpected exceptions?

Choosing properties for property-based testing

A handful of properties that you might try to apply to your code, along with examples of using them in F#.