Silverlight unit testing with NUnit: yes you can (without hacks)!

It may be obvious for most of you, but it took a while to my caveman brain to realize this, so I figured I could post it for other cavemen. You (we) have got an excuse though: the short release cycle of Silverlight means that most stackoverflow questions and blog posts on the subject are out-of-date and refer to older versions of Silverlight (<=3) when what I describe was not possible.

Up to Silverlight 3 you had to use a Silverlight-specific unit testing tool, like the Silverlight Toolkit framework. These tools are quite awesome and have been fundamental, but it’s nice to have the full power of NUnit (or xUnit & co.) at disposal. In addition to the community and tooling support, it’s practical to use the same tool you already use for other .NET projects.

The game changer is called binary assembly compatibility, brought by Silverlight 4. In a few words this means that you can add a reference to a Silverlight assembly from a “full” .NET project (provided that you don’t use any Silverlight-only class).

If your application is correctly layered (for ex. with MVVM) in most cases it’s trivial to keep views and viewModels/models in separate assemblies. ViewModels and models usually don’t reference any Silverlight-only class (otherwise you may have a code smell!) and are 100% compatible with .NET, so testing them with NUnit is as easy as

  • create a .NET class library in your solution
  • add a reference to NUnit (NuGet it!)
  • add a reference to your model and/or viewModel assemblies
  • write your unit tests
  • run them with NUnit

no tweaking or hacking required and works fine with F# assemblies as well.

It is typical for views to use Silverlight-only classes, but this is generally not a problem because it doesn’t make much sense to unit-test them anyways as they are mostly XAML with very little amounts of code-behind.

Free tip: if you want to test internal classes and methods you can add the InternalsVisibleTo attribute to the target assembly.

Happy testing!

bear

3 Responses to Silverlight unit testing with NUnit: yes you can (without hacks)!

  1. Jasper says:

    Do you have any sample project ? Somehow I can not build NUnit project according to your instructions… a lot of referenced dll missed for SL….

  2. Chuck Andersen says:

    I have a medium sized system with 100 or so classes, reasonably fitting the MVVM pattern. The problem I am having is testing the ViewModel classes that have ObservableCollections using NUnit or MSTest. Since that is defined in system.dll for the .net project required for either test framework, and in System.Windows.dll for the SL5 ViewModel classes, what can I do?

    Is this a caveman question? I’ll take a caveman answer.

    Thank you, gracias

    • frenk says:

      I suppose if you are having this kind of problems the safest approach is to add a new WPF class library project to your solution, recreate the exact folder structure of your ViewModel project, and add all its files with the “Add as link” option. You may have to #if SILVERLIGHT out some parts if they specifically use Silverlight-only classes, but it usually doesn’t happen often. That way you have a WPF class library that shares the same code (in terms of .cs pure text files) with the SL viewModels, but gets compiled to a “true” WPF dll. NUnit won’t have any problem anymore. In my experience this works better and I’m recently using this approach more than what I described in the post.

      Not sure I explained clearly, but it’s easier done than said. If I didn’t, please feel free to ping me again.

      Hope this helps!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>