Late last night I uploaded the RTM release of MSTestContrib 1.0.0 to the MSTestContrib project portal up on CodePlex. This release of MSTestContrib includes the ability to write BDD-style unit tests simply by sub-classing MSTestContrib.Specifications.Specification and decorating the class and methods with a few attributes.
The easiest way to get going with MSTestContrib is to install it using the NuGet package manager. If you have ASP.NET MVC 3.0 RC2 or above installed you already have NuGet installed. On the NuGet console issue the command “install-package MSTestContrib” with the unit test project selected as the default project.
If you don’t have access to NuGet you can still download a zip file that contains just the MSTestContrib DLL from the project portal releases page. The MSTestContrib.dll file needs to be referenced by the unit test project. Once this is setup you can create a new test class as per the normal process and then sub-class MSTestContrib.Specifications.Specification and add some attributes. The test method implementation just uses a series of methods to build up the BDD-style statement. The following code sample (gist 760628) shows a specification without any implementation detail provided.
Running this test using MSTest will produce an inconclusive result. The result summary view functions as you would normally expect.
Drilling in a bit closer to the individual test result you can see that MSTestContrib has reproduced a textual representation of the BDD-style test, spelling out the specification description, scenario description and the various BDD statements. At the end of each statement there is a “NotImplemented” indicator which is the cause of the inconclusive test result.
The following code sample (gist 760641) shows a completed test specification including not only the plain text description of the test case but also a function/expression that implements that description.
If the Add(…) method on the calculator threw an exception then the test result output would provide not only the text-based representation of the specification but also all of the exception detail. The following screenshot provides an example of the output.
It’s not pretty but there are limits to how much formatting you can put into the error message within the MSTest framework.
MSTestContrib Goals and Non-Goals
I wrote MSTestContrib primarily as a BDD-framework that I was happy using. There are dozens of BDD frameworks out-there including some really great ones. I was inspired by StoryQ but I wanted something that was simpler still. By taking dependencies on MSTest (the out of the box unit testing framework that comes with Visual Studio 2010) I hope to reduce barriers to entry for developers who have not done BDD-style development before.
Over time I am hoping that MSTestContrib will be extended with other framework elements which support teams that are investing in MSTest. If Microsoft produces tooling in MSTest which makes elements of MSTestContrib redundant then those features will be depreciated.