Sunday, 6 March 2011

Unit Testing for SharePoint - Part 1

Overview:  I am not an expert in Test Driven Development (TDD) but the SharePoint Retreat in London has made me think about using this approach for SharePoint 2010 development.  There are various tools to help you perform TDD however, these 2 posts show the approach I am most comfortable with thanks to Andrew Woodward. 

This whole article is based on Andrew's session and I really would recommend to any serious development team to get Andrew in to do some upfront consulting and potentially training.  Upfront costs and his experience would definately be quickly recovered.  If you have already decided to use a TDD get Andrew in to get you dev team up to a good standard with TDD for SharePoint, anyway enough of my sycophantic

  • Visual Studio (VS) 2010 any edition;
  • NUnit - preferable to MSTest for SharePoint dev
  • TestDriven.Net (optional)
  • ReSharper (optional)
Part 1 focuses on using setting up a basic "Magic 8 Ball" to unit test the business logic using nUnit. 

Tip: using coding Katas (really good approach to training, this is the 1st time I've used them and they actually really work) to get a full unit testing system working. 
Tip: Write the unit test and then write the code to fix the test.

At the SharePoint retreat, each session started with a:
  1. Review of the last sessions code (if appicable),
  2. Followed by an overview of what will be the next bit of coding logic (instructor lead teaching/demo exercise),
  3. Break into a new pair programming team to code (old code is throw away and a fresh set of code is started that includes all the exercise from all the previous sessions), and
  4. Loop - Back to step 1 (Review each pairs code).
In this post I am describing the 1st session, what you are trying to do is setup unit testing and write a couple of tests for you "Magic 8 Ball". 

1. Open VS2010, create a new solution file (MagicPost1.Sln).
2. Create 2 new C# class projects, the 1st project contains the business logic code (BOL project) and the 2nd project contains the unit tests (BOLTest project).
3. Change the default class on the BOL project to Ball and the default class on the BOLTest project to BallTest.
4. Add 3 references to the BolTest project. 
All 4 steps are shown below:

dll reference locations:
\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\lib\nunit.core.dll
\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\framework\nunit.framework.dll

5.  Add the basic testing structure to you BallTest.cs code as shown below:
6. Add this basic code snippet to your Visual Studio code snippets (Resharper has better code snippet support).  7. Write you 1st unit test (the simpler the better) as show below:
8. Build your project to ensure it compiles. 9. Comptete the test code as shown below and run the test.
Tip: Strictly following TDD principals your test should fail and then you write code to fix it in the BOL code.

More Info:
Andrew Woodward's codeplex project on the magic 8 Ball which provides the code you can use to build the magic 8 Ball with TDD.
Here is a slide deck to a presentation on the Magic 8 Ball TDD project.

Next Post:
Part 2 of the series looks at testing SharePoint using Type Moch Isolator.


Anonymous said...


I can't access the sharepoint site on NUnit.

The error was,

The Web application at [URL] could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

Any Idea?

Muthuraman M

Post a Comment