Can we just ignore the fact that the Getting Started post is my third in the series on FsCheck? This is what happens when I just start blogging about interesting things without planning out the series of posts in advance.

FsCheck is a tool for randomly generating test data for your unit tests. To install it into your own projects, look for the NuGet package FsCheck. The source code for FsCheck is written in F# (hence the name), and it makes for interesting reading. The documentation and examples for C# developers are a little sparse, so some experimentation is required to get started; fortunately the LINQ style syntax is pretty straightforward once you get going.

The [Property] attribute is part of the NuGet package FsCheck.xUnit, a package that integrates FsCheck with the xUnit testing suite. There’s also an FsCheck.NUnit package, though I haven’t used that.

If you don’t (or can’t) integrate with either of those testing tools, you’ll need to look into other ways to run your property tests. I’d recommend starting with the FsCheck QuickStart and reading the entire manual (it’s not that long).

Instead of the Assert methods built into xUnit, I prefer to use the ones from the Fluent Assertions NuGet package. Here’s a quick comparison of the differences:

// xUnit Style
Assert.True(tag.Equals(tag));
Assert.False(tag.Equals(tag));

// Fluent Assertions Style
tag.Equals(tag).Should().BeTrue();
tag.Equals(other).Should().BeFalse();

Fluent Assertions really begins to shine when you start doing more complex assertions - but that’s a post for another day.

Comments

blog comments powered by Disqus