liveBook · Manning

Property-based testing

As explained in F# in Action: A Revised Edition of Get Programming with F#

A property-based test is an individual automated test that evaluates one or more stated properties over generated inputs.

Property-based testing checks stated properties of code across generated inputs instead of comparing one known input with one expected result. A library generates randomly distributed inputs, increasing their complexity, to test whether the properties continue to hold. section 14.5.2

A conventional test might call flipCase with Hello and check one result. A property-based test describes the relationship that flipCase must preserve for any input, allowing the test library to explore strings of different lengths and characters. section 14.5.2

Use property-based tests when:

  • The possible inputs are so large or varied that hand-written cases cannot provide enough confidence. section 14.5.2
  • Several properties together describe the required behavior more effectively than individual expected outputs. section 14.5.2
  • You can invest more writing effort for tests that examine many generated inputs rather than one or two selected examples. section 14.5.2

Testing flipCase

Instead of asserting a result for the single input Hello, state rules governing the relationship between each input string and the output of flipCase. FsCheck then starts with simple strings, such as a single letter, and progressively tries more complex strings. section 14.5.2

Figure 14.3 Testing a function by proving that for any given input, it exhibits a set of rules or properties that govern the relationship between the input and output

Compare an explicit expected-output test with a test that proves rules governing the relationship between input and output. section 14.5.2

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains generated-input testing and a flipCase example, but does not specify the individual properties or their implementation.
Examples
Contrasts a Hello test for flipCase with progressively generated strings governed by input-output properties.
What it assumes
Assumes familiarity with conventional unit testing while introducing properties as an alternative to known expected results.
What you can do after
Apply it via FsCheck to test behavioral rules across many generated inputs.
How it connects
Connects property-based testing with unit testing, FsCheck, pure functions, and higher-order functions.

Also explained in

Compare how 8 books explain property-based testing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.