Unit Testing with SharePoint Web Parts

In my experiences, one of the best things you can do (if you do any XP practice at all) is to write Unit Tests. These are a small, focused set of tests that perform some specific testing of the system. For an overview of Unit Tests check out this link here. In the .NET space, NUnit is my preferred Unit Test framework. Your milage may vary.

When writing Web Parts for SharePoint, you can apply the same practice to your web part and exercise the Unit Tests using the NUnit GUI (or NUnit command line if you prefer). First download NUnit from here and install it to your development environment and on the SharePoint server.

How you setup your tests are up to you (and what dependancies your web parts will need) but try to position the Test Fixture up as high as you can on the SharePoint tree, holding whatever information you need. Get a reference to the entire web collection if you want so that way your tests don't have to tax the SharePoint server too much when running. Then just manipulate the collection during your tests. As long as you clean up after yourself (like deleting a site after creating it) everything should be fine.

Here's a sample test for a custom webpart that creates new sites (using the SPWebCollection class). The Test counts the number of existing sites and stores it and then adds a new site and tests the count (you could also find the test using a query or something but this is a simple example). The test uses data from the web part for input into the site creation process (name, description, etc.):

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;
using NUnit.Framework;
using CustomWebPart;

namespace CustomWebPart.Test
{

// This is our custom web part which
// has some public properties for the new site
private CustomWebPart webPart;
private SPWeb webSite;
private SPWebCollection siteCollection;
private string currentTemplate;

[TestFixtureSetUp]
public void Init()
{
    webPart = new CustomWebPart();
    webSite = new SPWeb(“http://Server_Name//sites//Site_Name“);
    siteCollection = webSite.Webs;
    currentTemplate = webSite.WebTemplate;
}

[Test]
public void TestAddSite()
{
    string siteName = webPart.SiteName;
    string siteDescription = webPart.SiteDescription;
    string siteUrl = webPart.SiteUrl;
    int siteCount = siteCollection.Count;
    // Add the new site
    siteCollection.Add(siteUrl, siteName, siteDescription, Convert.ToUInt32(1033), currentTemplate, True, False);
    int newCount = siteCollection.Count;
    Assert.AreEqual(siteCount+1, newCount, “Invalid site count“);
    // delete the site we just created
    siteCollection.Delete(siteName);
}

}

You can reference any part of your Web Part or invoke a method on it to help with your tests as needed. As well, once you create a connection to a SPSiteCollection or SPWebCollection you can iterate through sites or get individual sites and perform any tests on them. Just keep your tests clean and simple and only test what you need to as it relates to your Web Part.

No Comments