SharePoint Unit Testing

Unit testing Solution for Sharepoint – Typemock Isolator

Looking for a low-cost solution for SharePoint testing? 

Typemock Isolator for SharePoint makes it easy to test your web parts or any other SharePoint components (including support for SharePoint 2010). Isolator for SharePoint’s low cost means you can start testing your SharePoint applications now, and when you’re ready, you can upgrade to the full Isolator.


Typemock Isolator makes it easy to test code that is dependent on SharePoint, without using the SharePoint framework, so you can test your code (including WebParts) without changing it.

The problem with unit testing SharePoint
SharePoint is a very popular development platform. But it was not designed with testability in mind. It’s not possible to replace the different objects with hand-rolled mocks, and its “read-only” mentality makes it hard to set up to return the values you need for testing.

In order to test SharePoint code, you need to perform long operations like retracting and deploying just to get ready to test. In this sort of the environment, unit tests’ greatest value – immediate feedback – gets lost, and therefore the developers are discouraged from doing unit testing.

Typemock Isolator’s solution
Unit testing SharePoint code requires isolation from the framework. With Typemock Isolator you can write unit tests for your logic without changing it. Isolator helps simulate the SharePoint framework (without even the need to having it installed) so your tests run locally, quickly and give you the confidence your code works correctly…even before it loads into the browser!


Isolator’s SharePoint unit test in action


Let’s look at an example. We have a Web Part which contains a label that tells us the number of messages in the SharePoint list “Messages”.  Here’s the class:

Our main logic is the GetMessageNumberText method. However, there are a number of problems with testing:
•    The method is private.
•    We need a site called “www.nosuchsite.com” just for the test.
•    The SPSite object is created inside the code-under-test and cannot be replaced with another one.
•    For testing the two cases (no messages and new messages) we’ll need to setup the SharePoint list differently.

Let’s see how we can easily and quickly test it with Isolator:

Note that we don’t need SharePoint running or even installed: we just need the SharePoint DLLs to compile the tests. We fake the entire SharePoint object set with just a few lines (even the SPSite object), invoke the private method with a short API and test the result.

Download a free trial of Typemock’s SharePoint Unit Testing tool to start writing your own SharePoint tests or click through our resources below for all the information and support you need to effectively unit test in SharePoint.

Download Typemock Isolator SharePoint Unit Testing Tool!