loading...
Cover image for How to get started with Property-based Testing in C#

How to get started with Property-based Testing in C#

mbernard profile image Miguel Bernard Originally published at blog.miguelbernard.com ・4 min read

Property-based testing (5 Part Series)

1) What's wrong with the current state of unit testing 2) How many unit tests are good enough | Property-Based testing 3) How to get started with Property-based Testing in C# 4) Input generators in property-based tests with FsCheck 5) First and Last line content | Solving the diamond kata with property-based testing

Solving the diamond kata with property-based testing series

  1. How to get started with Property-based Testing in C#
  2. Input generators in property-based tests with FsCheck
  3. First and Last line content
  4. Height equals Width
  5. Outside space symmetry
  6. Symmetry around the vertical axis
  7. Symmetry around the horizontal axis
  8. No padding for input letter row
  9. Letters order

All code samples are available on github

Intro

So far, we've talked about the benefits of property-based tests in my two previous blog posts here and here. I'm sure for most of you; this concept is still pretty abstract. We've seen the technique, but how do we implement it in C#? In this series, we'll go through all the steps to solve the Diamond Kata using TDD and property-based tests.

The Diamond Kata

Given an input letter, print a diamond starting with A and having the supplied letter at the widest point.

e.g., For input: D

---A---
--B-B--
-C---C-
D-----D
-C---C-
--B-B--
---A---

Why did I choose this specific Kata? Well, if you try to solve it with example-based tests, you effectively need 26 tests to make sure it works properly. It may not seem like a lot of tests, but if you try it, you'll soon discover that the first few tests are trivial, and as you approach Z, the tests are becoming harder to read and harder to write. It's usually a code smell that indicates that some property-based tests would be a better approach.

TDD

In this series, we'll also use TDD, and you'll see that it's much easier to use with Property-based tests than example-based tests. So in excellent TDD practitioners that we are, we are going to start with the most straightforward implementation of the algorithm that we can think of.

public class Diamond
{
    private static IEnumerable<string> Generate(char c)
    {
        yield return "A";
    }
}

Easy, right? It only works for the A case, but it's a good starting point.

I already hear the purists among you.

This is not the right way to do TDD! Red -> Green -> Refactor! You should start with a failing test, not with the implementation.

You are not wrong, but let's be realistic here. It's just a starting point, and I promise we'll do better TDD going forward :D.

The tools

Property-based tests is a simple enough concept, but generating proper pseudo-random inputs can be harder than it looks. Fortunately for us, other people solved that issue and built great tools. In our case, we'll use FsCheck, a property-based testing framework made for F#.

I'm confused, are we doing this in C# or F#?

That's the beauty of .NET, even though the library is made for F#, all the .NET languages compile to Intermediate Language (IL or MSIL) and thus are interoperable. It means we can call that library from C#.

Install the NuGet packages, and you are ready to go.

dotnet add package FsCheck --version 2.14.2
dotnet add package FsCheck.Xunit --version 2.14.2

Closing word

I think we have all the prerequisites to get started. Stay tuned for the next post where we'll implement our first test.


Solving the diamond kata with property-based testing series

  1. How to get started with Property-based Testing in C#
  2. Input generators in property-based tests with FsCheck
  3. First and Last line content
  4. Height equals Width
  5. Outside space symmetry
  6. Symmetry around the vertical axis
  7. Symmetry around the horizontal axis
  8. No padding for input letter row
  9. Letters order

All code samples are available on github

Property-based testing (5 Part Series)

1) What's wrong with the current state of unit testing 2) How many unit tests are good enough | Property-Based testing 3) How to get started with Property-based Testing in C# 4) Input generators in property-based tests with FsCheck 5) First and Last line content | Solving the diamond kata with property-based testing

Posted on by:

mbernard profile

Miguel Bernard

@mbernard

Miguel has over 10 years of experience with Microsoft Technologies. Specialized with many things in the Microsoft ecosystem, including C#, F#, and Azure

Discussion

markdown guide