November 20, 2010 by Alistair Deneys
I was chatting to another Sitecore developer the other day about unit testing in Sitecore. The impression I got from the developer was that although they knew unit testing could be done, they felt it was quite cumbersome and difficult. Possibly the length of my previous series on unit testing in Sitecore didn’t help the cause either.
So I’d like to show you just how easy unit testing in Sitecore is. One reason my previous series was so long was because it covered a lot of topics and areas related to unit testing and how to test various UI components in Sitecore as well as different strategies for setting up and pulling down your test data.
Unit testing of UI is not as important as unit testing of business logic and utility methods. These are the easiest pieces of any system to test and it’s where you should spend you initial time, especially if you’re trying to introduce unit testing on your projects for the first time. Your UI components would need testing though if they themselves contained a level of business logic. And I’m not talking about heavy business logic, more the things like altering or filtering the output based on parameters passed to your component (especially relevant for XSLTs).
So I’ve recorded a video showing exactly how to setup unit testing on your Sitecore project using my custom NUnit test runner. I’ve found it much easier and safer to use this custom test runner rather than trying to fake the HttpContext or mocking. I’m a strong believer that you shouldn’t try and mock something unless you have intimate knowledge of the component being mocked and can have it changed if it doesn’t meet the assumptions your tests and mocks were built around. Mocking an entire system such as Sitecore or SharePoint is fraught with danger and it’s likely that you think you have a good idea about how the system works, but you don’t have the ability to change it if an assumption you’ve baked into your mock is wrong. Part of the unit tests running inside the system is to validate your understanding of the system.
The process I show in the video is:
- Download the custom test runner
- Include the test runner it in your test project
- Configure the build using MSBuild
- View the test runner
This is probably the simplest form of unit testing for your Sitecore projects.
In the next video I’ll show how to start writing your unit tests.