codeflood logo

Sitecore Integration Testing Made Easy

A few years back, I uploaded a couple of short videos to YouTube to show how easy it can be to get started with testing your Sitecore solution. The approach shown in the videos uses my ASP.NET embedded test runner. I developed the first version of this test runner when I was first trying to test my own Sitecore projects, many years ago. The issue many developers face when trying to test their Sitecore solutions is trying to get the Sitecore code to run outside of a web/Sitecore context. What I managed to do with this test runner was flip the problem on it's head; instead of trying to get the Sitecore code to work inside my unit test, I made my unit test work inside my Sitecore solution. As long as we can ignore the semantics, then this is a very easy way to get started with testing your Sitecore solutions. Yes, these tests are not actually unit tests, but integration tests. But don't let semantics get in your way. If you're struggling to get started with testing your Sitecore solutions, then this is by far the easiest way to get started, in my opinion. Recently I've delivered presentations to a few user groups on testing, and how to get started testing your Sitecore solutions. In amongst the other techniques I demonstrated, it was apparent that I could make the embedded test runner approach even easier with a little NuGet (I used to just add the files from a zip). So, my embedded test runner is now available via NuGet. To add the test runner to your existing web project all you need to do is install the package:

Install-Package codeflood.nunit.aspnet.testrunner

The package adds a Test.aspx webform to your project which is the actual runner itself. codeflood aspnet nunit test runner Once you deploy that file and your assembly to your running Sitecore instance (you do that automatically through MSBuild/grunt/gulp right?) then all you need to do is access the Test.aspx file in your browser. The test runner operates on the assembly in which it's compiled (your project), so it doesn't require any configuration to tell it which file your tests are in. The test runner has also been extended to allow filtering individual test methods, as well as the category filtering it has always done. In releasing the test runner on NuGet I've also created a github project to hold the code and track feature requests. I already have a few updates planned to make the test runner callable from build servers for CI, and output results in machine friendly formats to assist as well.

Gimme the Steps

OK, just to make sure I cover off exactly how to use the test runner and show how easy it is to start testing your Sitecore code:

  1. Create a new empty web project
  2. Configure the project to deploy to your local Sitecore instance
    1. Ensure the project assemblies and all content files are deployed
    2. You can do this automatically using MSBuild, grunt or gulp, or you can configure the "publish" settings of your project
  3. Install the codeflood ASP.NET NUnit test runner package from NuGet:
Install-Package codeflood.nunit.aspnet.testrunner
  1. Add your test classes
  2. Compile and deploy your project
  3. Access the Test.aspx file from your running Sitecore instance
http://(your-project-url)/test.aspx
  1. Set any filters required and click the Run button

Not Just for Sitecore

Although I created this test runner for my Sitecore projects, it's not bound to Sitecore in any way. This test runner is useable in any project that has issues running tests outside a web context (old school SharePoint anyone?).

Conclusion

And it's as simple as that. Demonstrating this on stage, I had my test project created and running my first test in under 3 minutes. Just please, don't let the semantics get to you. Just start testing :)

Comments

Leave a comment

All fields are required.