Lesson 41. Property-based testing in F#

 

This lesson presents a different type of automated testing that you can do in F#, called property-based testing (PBT). You’ll see

  • What PBT is
  • Why you might want to use it
  • How to use the FsCheck library for .NET
  • How to integrate FsCheck with popular test runners

At times, even if you’ve achieved “full test coverage”—where your tests are covering every branch of code in the system—you still might not be satisfied with your tests. Here are a few examples.

One common problem occurs when your code misses edge cases that you hadn’t considered. This is common when working with strings or other “unbounded” inputs. How many times have you written code that expects a string to be a certain minimum length, but somehow an empty string creeps into your code? Or you wrote code that indexed into an array that turned out to be empty? Are these cases that you should’ve realistically expected?

Another example occurs when your unit tests appear to be fragile because they test only arbitrary cases. Imagine that you have a method, half(), being tested that takes in a number and returns half of it. To test this, you write a single test case that proves that when calling half() with 10, you get back 5. Why did you pick 10 as the test data? Is it easy to understand what the test is really proving? Have you tested all valid cases?

41.1. Understanding property-based testing

41.2. Introducing FsCheck

41.3. Controlling data generation

Summary