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: http://nunit4teambuild.codeplex.com/ 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:

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 *