In SharePoint Development, Unit Tests are hard to build due to the large number of dependencies on SharePoint Development in general."unit testing is a method of testing that verifies the individual units of source code are working properly. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a method, which may belong to a base/super class, abstract class or derived/child class"
Wikipedia - Unit Testing
There are multiple approaches to introducing Unit Testing into SharePoint Development:
- Test Driven Development
- Extreme Programming
Pros and Cons of Unit Testing in SharePoint Development
| Pros | Cons |
|---|---|
| Encourages TDD development | Lots of plumbing - More code and complex project structures |
| You know when something breaks in an intricate system | Negative and Positive - how far to go with this is hard to define e.g tests for invalid urls |
| Communicates funcionality in team development | People only test code they know works |
| People tend to use it as integration test rather than discrete unit tests |
Getting Started with SharePoint Unit Testing
Prerequisites
Setup
What can be tested?
SharePoint Development with Unit Testing webcast
The SharePoint community has currently moved toward Test Driven Development but below are some useful links regarding Unit Testing in general.
Other approaches
sporm
sporm
is a CodePlex project that has appeared in Alpha as of Aug 09. It takes the approach of building an ORM across a site collection which you consume rather than the API directly.
Traditionally SharePoint projects are hard to unit test, use weak typing, and require complicated XML to query for data. Sporm (TM) is specifically designed to simplify unit testing, enable strongly typed objects, and support LINQ access to SharePoint objects.
Benefits
- Simplifies unit testing - by hiding unmockable objects like SPWeb and SPListItem
- Enables strongly typed code - by generating classes and properties for lists and content types
- Eliminates the need for ugly loosly typed CAML queries - by acting as a LINQ provider to your lists
External References
- Microsoft - SharePoint Guidance - Unit Testing

Microsoft has published an article on Unit Testing in SharePoint as part of the SharePoint Guidance - Andrew Woodward - 21Apps

Andrew has a great set of articles on Unit Testing in SharePoint Development including 2 excellent White Papers. - Roy Osherove - Art of Unit Testing

Roy has a book on Unit Testing for Development - Francis K. Cheung - Unit Testing SharePoint 2007 Applications

Francis discusses the approaches and also the reasons why SharePoint Unit Testing is so difficult. - SPTDD: SharePoint and Test Driven Development, Part One by Eric Shupps

- Spencer Harbar - "Test Driven" SharePoint Development

- Peter Seale - SharePoint: Not Unit Testing

- Testing SharePoint Workflows using TypeMock Isolator

- Unit Testing Sharepoint
- Unit testing SharePoint page on Typemock's site

