Doing Unit Testing with TFS2010 and SP2010

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:

        [Test]
        [Category("Features")]
        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) {
                Assert.Fail(exc.Message);
            }
        }

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

Advertisements

One Response to “Doing Unit Testing with TFS2010 and SP2010”

  1. Visual Studio 2010 SP1 available (including TFS 2010) « SharePoint Stef (@vanHooijdonk) Says:

    […] We now have .Net Framework 3.5 Unit testing. I’ll let you figure this one out ( here is a hint. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: