POKE 36879,255

I’ve found writing unit tests in F# very productive, with it’s terse syntax and powerful type inference. I write F# tests against C# and F# implementations.

Writing tests in F# can be a good introduction to the language and this is something we do on our team. To get up to speed quickly you’ll typically want a familiar unit testing framework and mocking library syntax.

In C# the return value of a method can be implicitly ignored. In F# return values must be explicitly handled. Simple test functions are typically expected to return nothing, so in F# you need to be careful that you don’t inadvertently return a value, for example NUnit’s Assert.Throws returns the specified exception which you can pipe to ignore. Or you can extend the Assert class with a Raise method that ignores the return value.

F# works well with many of the major .Net mocking libraries like Rhino.Mocks and NSubstitute. Built-in support for LINQ expression trees used in libraries like Moq was introduced recently with F# 3.0 in Visual Studio 2012. For F# 2.0 in Visual Studio 2010 the F# PowerPack is required which can be a bit fiddly.

Foq

Foq is an F# mocking library similar to Moq that parses F# Code Quotations, making it compatible with F# 2.0/3.0 and .Net 2.0/3.5/4.0/4.5.

Funny - I was just thinking yesterday of writing a blog post on F# and unit testing. It's not an aspect of the language that gets much press, but after using FsUnit for a while on F# projects, I find it adds lots of expressiveness to the unit tests, by comparison to raw NUnit, and it might actually be a nice entry point to the language for beginners. I found most C# developers are uncomfortable with the |> operator, but in the context of FsUnit, it is IMO extremely natural, more so than a classic fluent interface.
Nice job on Foq, haven't tried it yet but the code looks nice. And the name is pretty daring Does it support setting up to throw exceptions?

As there are plugins/extensions for NUnit and XUnit you can use now the inbuild VS2012 test runner (of course also with F# libs) and it's quite nice.
It comes with the ability to run the tests on build and it's clever enough to only rerun red tests on the next build.
Overall I now prefer it to NCrunch (the compile on each save just slows VS down to much for me as I have ReSharper installed already) - the only downside is it's somewhat poor output but I'm sure there is improvement on the was