Doing Unit Testing with TFS2010 and SP2010

By | 2010-06-22

SharePoint 2010 has a wonderfull new developers experience with Visual Studio 2010 and great integration with Team Foundation Server 2010. We configured our TFS Build server to build our SP2010 solutions fairly easy. Just make sure the SP2010 dll’s you reference in your SP2010 projects are available on the TFS Build Server (in the GAC makes it very easy).

But one thing is not possible, and that is using the Test Projects with your SP2010 projects. Why? because Microsoft did not implement testing with .Net framework 3.5 and below. You can only use the Test projects in VS2010 (and thus in TFS2010) with 4.0 Framework projects. And so you cannot use the Unit test in VS2010 either.

But you can fairly easy use the Nunit 2 framework with VS2010 and TFS2010. Head over to this codeplex project: and you can now have 3.5 Framework NUnit tests with TFS2010. And your Unittest results even show up in TFS2010 build reports!

So how to get NUnit to test your SP2010 projects? Install SP2010 on your TFS Build Agent! You can then deploy your solutions on the build server, perform unit tests, and then remove your solutions again.

Individual test results:

So what do you need:

– VS 2010 solution with one or more SP2010 projects and at least one NUnit Test project
– TFS Build Agent with SP2010 installed and configured
– A web application for this solution we want to build ( store the web application url in your tfsbuild.proj )
– a deployment script ( powershell off course ) that can be used on you dev server, the Build Agent server (farm) and other farms
– a removal script ( powershell also ) that clears your deployment again
– a tfsbuild.proj that builds, packages and runs the nunit ( see below )
– XSLT to transform NUnit test results to MSTest test results

A powershell script to deploy a solution can be found here
So here is a small overview of my tfsbuild.proj:

– build the solution/projects
– package the projects that are sp2010 projects ( this makes the wsp packages ! )
– run a powershell deployment script
– run nunit against your test project
– run a powershell removal script
– convert the nunit result xml to TFS / MSTest format with xslt ( see codeplex link above )
– upload the nunit transformed test results to tfs with MSTest
– zip the solutions with the scripts
– upload the zip to a location where you can use it on the test environment

Example NUnit code:

        public void TestFeatures() {
            Assert.IsNotNull(this.projectName, "No valid ProjectName found.");
            Assert.IsNotNull(this.webapp, "No valid Test WebApplication is found.");
            Assert.IsNotNull(this.testsite, "No valid Test Site Collection is found.");

            try {
                //string contenttypes_id = "03ec908f-fc7a-44cf-a8fc-bc3b9d9896de";
                //string trefwoorden_id = "f0455e8e-aa5d-4ae7-8af9-a97947f7e3a0";
                string webparts_id = "f0e1d4d6-2694-479f-8751-9704818f1464";

                SPFeature feature = this.testsite.Features[new Guid(webparts_id)];
                Assert.IsNotNull(feature, "webparts_feature not found active.");

                // check if the webparts are present in the WebPart Gallery

            catch (Exception exc) {

You can download exmple scripts and proj files here:
Codeplex “release” for this blog

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.