liveBook · Manning

FsCheck

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

FsCheck is a .NET library for property-based testing that generates input values and checks properties against them.

FsCheck is a .NET library for property-based testing. Instead of checking one known input and expected result, you describe properties or behaviors that should always hold. section 14.5.2

FsCheck generates randomly distributed inputs, beginning with simple values and increasing their length, complexity, and character variety. This explores thousands of cases where one-off unit tests might provide limited confidence. section 14.5.2

Use FsCheck when:

  • Standard unit tests do not cover a sufficiently large or varied input space. section 14.5.2
  • Several related properties together describe the required relationship between a function's input and output. section 14.5.2
  • You can invest more effort in tests to gain greater confidence in correctness. section 14.5.2

Testing flipCase

A conventional test might call flipCase with "Hello" and check one expected result. An FsCheck test instead states properties governing the relationship between any input string and its output, then checks those properties across progressively more complex strings. section 14.5.2

Figure 14.2 Testing a function with an explicit input and expected resultant output

Compare a single explicit input and expected output with property-based exploration. 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

Explore how rules about input-output relationships replace a fixed expected result. section 14.5.2

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

How deep it goes
Explains property-based testing through generated inputs, increasing complexity, and combined input-output properties.
Examples
Uses flipCase, the input "Hello," and progressively varied strings to contrast explicit tests with properties.
What you can do after
Apply it via FsCheck to evaluate correctness across many generated inputs instead of relying on a few hand-selected cases.
How it connects
Connects FsCheck with property-based testing, unit testing, testable functions, and pure functions.

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.