Silverlight Unit Test Headwords

At the time of writing, creating unit tests for Silverlight is not as smooth and easy as it is for .NET assemblies. My experiences base on the Silverlight toolkit published on Codeplex silverlight.codeplex.com.

  • You cannot access private or protected methods or properties. So use the #if DEBUG to make these parts of the class internal (in DEBUG mode).
  • Add [assembly: InternalsVisibleTo(“NameOfUnitTestAssembly”)] to the AssemblyInfo.cs of the library that contains the class to be unit tested. Of course, again only for DEBUG mode.
    Even access to non-public methods by reflection is not possible (security reasons).
  • For the unit test assembly, create a new Web project to host it. Otherwise, you need to switch the start page of the existing Web project.
  • Only create one unit test assembly for all Sliverlight assemblies you want to test. Doing so you will have a better overview.
  • Creating trace output is not possible. Although there is an ‘Output’ tag in the result page, there is no way to write output into it. Use the Debug.Write… and Sysinternals DbgView to view the trace if required.