Using FSpec with NCrunch

If you have the excelent Visual Studio plugin, NCrunch, you can have it run FSpec tests as you are writing your code – with some limitations, unfortunately.

The primary limitation is that NCrunch sees the entire test suite as one test. Therefore, it is not able to:

  • Display specifically which test(s) have failed, only if the entire suite is a success or not.
  • Only execute the tests that are specifically affected by a change in code, thus optimizing execution time.

But if your tests are fast, it will do its job for a normal red/green/refactor cycle.

How does it work

The integration is based on the fact that MbUnit supports “Dynamic Test Factories”, a class that can generate unit tests at run time, as instances of a TestCase class. And NCrunch has support for MbUnit (make sure that it is enabled)

FSpec provides a wrapper that can inspect an FSpec test suite and exposes it as a dynamic test factory.

But in order to be able to display the test suite in a hierarchical form, and execute a single test case, this structure has to be known at compile time – and for dynamic test factories, this is not the case; thus the limitations.

How to use it

In your spec assembly, add the FSpec.MbUnitWrapper NuGet package. Then add the following piece of code:

[<MbUnit.Framework.TestFixtureAttribute>]
type Wrapper() =
    inherit FSpec.MbUnitWrapper.MbUnitWrapperBase()

And that’s it. If you are running the latest NCrunch, and have MbUnit enabled in NCrunch, you should start seeing red/green dots for your code.