Unit Testing and Code-Coverage in Silverlight

Update: This post is useless. Instead, see this Stack Overflow question, which uses the official unit-testing framework by Jeff Wilcox.

Is it possible to test Silverlight applications using standard tools like NUnit and PartCover? I explored the possibility of, and ultimately solution to, this problem, with a resounding “yes.”

Those of you who played Valence may suspect that I’m not using traditional Silverlight tools. In fact, I’m using FlatRedBall, a 2D game engine that also supports porting to and developing in Silverlight.

With Valence over and complete, I turned to the next project. How could I improve productivity and (code) quality this time? Coming from a background of automated testing and TDD (Test-Driven Development), this begged the question: how can I both unit-test and generate code-coverage on my games?

A quick google showed that I need to either a) get Visual Studio Professional, or b) use the standard Silverlight application to generate the main application screen as a testing screen. Neither will suffice; nor is writing my own unit-testing tool appropriate, since it will lack integration with code-coverage.

An idea surfaced: just like we share code among Silverlight and non-Silverlight versions of libraries, couldn’t we share our model and controller code the same way, and unit-test using traditional tools like NUnit and PartCover?

The result is this solution here: Silverlight Testing with NUnit v1.0 (511.16 KB)

It’s a bit specific towards FRB, but nothing major. It contains five constituent projects:

  • View (also called Core): This is the FlatRedBall engine and the final game. You can disregard this; all you need to know is that it references the model/controller project. It builds my game code files.
  • Web: Another FRB-specific project, it generates the XAP file from the View/Core project code.
  • ModelController: My model and controller classes; they build to a Silverlight DLL.
  • NonSilverlightModelController: A non-Silverlight DLL project that references all my model and controller classes. This is the mirror of my Silverlight code, as non-Silverlight code, since my application domain (game entity classes and controller/utility classes) are not using any Silverlight-specific API.
  • AutomatedTests: A plain, vanilla NUnit project with a couple of test-cases for some of my ModelController classes. It references the non-silverlight ModelController.

And that’s it! I can develop my model and controller code independently of my view (always good for MVC). I can create NUnit tests against the non-Silverlight DLL. I can generate tests and code coverage. And I have the confidence that my Silverlight code is tested. The only exception is the view code — and I strongly oppose automated UI testing anyways due to fragility and high maintenance.

So you can use this as a template for your own projects — you can use standard tools and, with a little thought up-front and reorganization, test and code-cover most of your application code using standard tools.

Note that the downfall of this is that I cannot test Silverlight-specific code, like IsolatedStorage-consuming classes. For that, I may well end up writing my own version of NUnit, since it’s possible to use NUnit to invoke Silverlight code.

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Core .NET, Silverlight, Tools and tagged , , , . Bookmark the permalink.

One Response to Unit Testing and Code-Coverage in Silverlight

  1. Pingback: News Intranet » Blog Archive » http://www.csharpcity.com/unit-testing-and-code-coverage-in-silverlight/

Leave a Reply

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