Unit testing is important, even for Silverlight applications. At my job, I insist that every application contain at least some unit testing (that doesn’t mean that every application does, but I’ll keep insisting), and that we maintain a minimum of 40% code coverage. Normally, I implement unit testing with NUnit. But NUnit doesn’t work with Silverlight:
Fortunately, there are other options. For one, we have the Silverlight Unit Test Framework:
The Microsoft Silverlight Unit Test Framework (Microsoft.Silverlight.Testing) is a simple, extensible unit testing solution for rich Silverlight 2 applications, controls and class libraries.
The biggest problem I see with the Silverlight Unit Test Framework is that it lacks the ability to automate the tests. Once the tests are added, someone has to run them and examine the results. In other words, there’s no “nunit-console.exe” to run as a post-build step. What I really need is NUnit for Silverlight…
Enter Jamie Cansdale of TestDriven.NET fame, who rigged up a Visual Studio NUnit template that includes a Silverlight-compatible version of the NUnit.Framework assembly. The main drawback is that Jamie does not provide the converted NUnit source code. But if you think of this as a stop-gap measure until NUnit 3.0 arrives, then it should work nicely for you.
All that being said, as Silverlight developers we are presented with two unit testing options:
- Jamie’s Silverlight NUnit port for automated, every-time-you-build-unit tests
- The Silverlight Unit Test Framework for integration and GUI testing
There may be others, but this is what I’m principally going to focus on for now.
Jamie’s Silverlight NUnit port: How to Get Started
Download Jamie’s .vsi template linked to on his post. Double-click the .vsi to install, selecting “Yes” when the warning dialog pops up.
Open any Silverlight project (or create a new one), right-click to add a new project, and you’ll see:
That gives you the following project layout:
SilverlightTests.cs starts with a basic unit testing framework which you can then start modifying and adding to:
1: using System;
2: using System.Reflection;
3: using NUnit.Framework;
5: namespace SilverlightNUnitProject2
8: public class SilverlightTests
11: public void SystemVersion ()
13: Assembly systemAssembly = typeof (Uri).Assembly;
14: StringAssert.Contains ("=126.96.36.199,", systemAssembly.FullName,
15: "Check we're testing a Silverlight 2.0 assembly");
If you’ve used NUnit before you should be in familiar territory now. I like to set “nunit-console.exe” to run as a post-build step so that every time I perform a build, the unit tests are run. Something like:
“C:Program FilesNUnit 2.4.7binnunit-console.exe” $(TargetPath)
That’s it for Jamie’s Silverlight NUnit port. I hope he either makes the source available or integrates the functionality into TestDriven.NET.
The Silverlight Unit Test Framework: How to Get Started
There’s a lot of great tutorials on how to get started using the Silverlight Unit Test Framework. Jeff Wilcox, who is on the Silverlight Toolkit team, posted the introductory one back in March, 2008. It’s good stuff, and worth a read. I won’t try to repeat his or others’ work, but instead just point out a couple of things, especially as we are now on Silverlight 3 and not 2.
First, while Jeff has some Visual Studio templates available for download on his post, note that they are out-dated. Instead, go to the Silverlight Unit Test Framework home page and look for the download new templates link (it’s not the most conspicuous thing on the page):Once you’ve got the .zip downloaded and opened, you’ll see zip files for C# and VB:
Extract whichever language you’re interested in (or both) to your Visual Studio 2008 templates folders, putting the “test class” under ItemTemplates and the “test project” under ProjectTemplates. You can even take it one step further by creating a “Silverlight” sub-folder under the language folder. For example, I put the “test project” zip here:
Which has the effect of displaying the “Silverlight Test Project” under the Silverlight folder when I go to add a new project:
Similarly, with the “test class” template:
One last point: If you’re looking for the Silverlight Unit Test Framework binaries corresponding to Silverlight 3 and you’ve already downloaded the Silverlight Toolkit, then you’ve already got the new unit test assembles.