Input generators in property-based tests with FsCheck
Tags: testing, csharp, dotnet
Published: 2020-07-03 12:04:40

# [Solving the diamond kata with property-based testing series](https://blog.miguelbernard.com/solving-the-diamond-kata-with-property-based-testing-series/)

1. [How to get started with Property-based Testing in C#](https://blog.miguelbernard.com/how-to-get-started-with-property-based-testing-in-csharp/)
1. [Input generators in property-based tests with FsCheck](https://blog.miguelbernard.com/input-generators-in-property-based-tests-with-fscheck)
1. [First and Last line content](https://blog.miguelbernard.com/first-and-last-line-content)
1. [Height equals Width](https://blog.miguelbernard.com/height-equals-width/)
1. [Outside space symmetry](https://blog.miguelbernard.com/outside-space-symmetry/)
1. [Symmetry around the vertical axis](https://blog.miguelbernard.com/symmetry-around-the-vertical-axis/)
1. [Symmetry around the horizontal axis](https://blog.miguelbernard.com/symmetry-around-the-horizontal-axis/)
1. [No padding for input letter row](https://blog.miguelbernard.com/no-padding-for-input-letter-row/)
1. [Letters order](https://blog.miguelbernard.com/letters-order/)

> All code samples are available on [github](https://github.com/mbernard/property-based-testing)

---

# Intro

Now that we have all the [tools installed](https://blog.miguelbernard.com/how-to-get-started-with-property-based-testing-in-csharp/), we are finally ready to write our first Property-Based test.

## FsCheck

Creating a property-based test is as simple as tagging a method with the `[Property]` attribute, similarly as you would do in xUnit with the `[Fact]` or `[Theory]` attributes. If you dig a little, you will realize that there's a lot of configurable options on the `[Property]` attribute. You can control how many random tests will be run each time, timeouts, etc. One of them is essential; the `Arbitrary` property. That property will define which method to use to generate random inputs. In our specific case, we are not interested in all the possible values of `char`. We only care about the 26 letters of the alphabet. To scope the input, we'll need to configure a generator that will do just that.

## Generators

Generators are relatively simple. 
1. Create a static class
1. Add a static method that returns `Arbitrary<TypeYouWantToGenerateValueFor>`
1. Voilà

> You can write your randomization logic, but it's usually better to start from the built-in methods as they are already well balanced.

```csharp
public static class LetterGenerator
{
    public static Arbitrary<char> Generate() =>
            Arb.Default.Char().Filter(c => c >= 'A' && c <= 'Z');
}
```

## Not Empty Test

It may seem basic, but it's quite essential. We want to make sure we always have a non-empty result.

```csharp
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property NotEmpty(char c)
{
    return Diamond.Generate(c).All(s => s != string.Empty).ToProperty();
}
```

One thing I forgot to mention earlier, the test method also needs to return a `property` object.  Fortunately for us, the library contains an extension method that can transform any boolean expression into a property object by calling `ToProperty()`.

So our test reads like this:
* Call the [Diamond.Generate()](https://blog.miguelbernard.com/how-to-get-started-with-property-based-testing-in-csharp/#tdd) method with the provided input char `c`.
* Make sure all the lines of the returned diamond are not empty.
* Use the `ToProperty()` method to wrap it into a property object.

## Stay tuned
Stay tuned for my [next posts](https://blog.miguelbernard.com/first-and-last-line-content) on this [series](https://blog.miguelbernard.com/solving-the-diamond-kata-with-property-based-testing-series/) about solving the diamond kata with property-based testing. The [following post](https://blog.miguelbernard.com/first-and-last-line-content) will try to define properties for the first and last lines of our diamond.

---

# [Solving the diamond kata with property-based testing series](https://blog.miguelbernard.com/solving-the-diamond-kata-with-property-based-testing-series/)

1. [How to get started with Property-based Testing in C#](https://blog.miguelbernard.com/how-to-get-started-with-property-based-testing-in-csharp/)
1. [Input generators in property-based tests with FsCheck](https://blog.miguelbernard.com/input-generators-in-property-based-tests-with-fscheck)
1. [First and Last line content](https://blog.miguelbernard.com/first-and-last-line-content)
1. [Height equals Width](https://blog.miguelbernard.com/height-equals-width/)
1. [Outside space symmetry](https://blog.miguelbernard.com/outside-space-symmetry/)
1. [Symmetry around the vertical axis](https://blog.miguelbernard.com/symmetry-around-the-vertical-axis/)
1. [Symmetry around the horizontal axis](https://blog.miguelbernard.com/symmetry-around-the-horizontal-axis/)
1. [No padding for input letter row](https://blog.miguelbernard.com/no-padding-for-input-letter-row/)
1. [Letters order](https://blog.miguelbernard.com/letters-order/)

> All code samples are available on [github](https://github.com/mbernard/property-based-testing)