visit
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.You can write your randomization logic, but it's usually better to start from the built-in methods as they are already well balanced.
public static class LetterGenerator
{
public static Arbitrary<char> Generate() =>
Arb.Default.Char().Filter(c => c >= 'A' && c <= 'Z');
}
[Property(Arbitrary = new[] { typeof(LetterGenerator) })]
public Property NotEmpty(char c)
{
return Diamond.Generate(c).All(s => s != string.Empty).ToProperty();
}
Previously published at