Press "Enter" to skip to content

Unit Testing SharePoint 2010 with MSTest

Most have figured out by now that Visual Studio 2010 and SharePoint 2010 have offered much better development stories compared to SP 2007 and VS 2008. Unit testing however, seemed to be worse off. MSTest projects only support .NET 4, and if a Visual Studio solution is set to .NET for, the SharePoint assemblies won’t work at all. You will get a System.IO.FileNotFoundException if you try to get reference to an SPSite. (There’s really good reasons why it is a FileNotFoundException, but thats for another post.)

This resulted in many loud cries in the industry…

With the release of Visual Studio 2010 SP1, along came a resolution to this problem. MSTest projects now support .NET 3.5, which lets us add reference to SharePoint assemblies and execute the test host in an x64 process.

Here’s how to do it:

1) Create a new Test project in your solution. Make sure you select the .NET 3.5 Framework option. Take note of the location the template is in, not sure what Test Documents is

2) Go to the Properties of the newly created project and confirm that the Target framework is .NET 3.5

3) Set the Build – Platform target to “Any CPU”. This is important, it doesn’t work when explicitly set to x64, I haven’t got around to figure out why.

4) Go to the Test menu at the top, select Edit test settings, and choose local settings:

5) Click the Hosts option and configure the environment to run under x64 bit:

5) Add references to you SharePoint assemblies and write some unit tests. They should run fine:

Great stuff from the VS team. This still doesn’t solve the stubbing requirements though. Pex and Moles and TypeMock are still the way to go.

Another major functionality that is still missing is Load Tests in .NET 3.5 mode, still can’t do them 🙁

Hope this helps!