Silverlight Testing

Now I've got the simplest application up and running, I'd like to add some testing around it. At the moment there's not a great deal to test, but I want to get it all setup so I can go the TDD route.

NUnit?

I've used NUnit for quite a long time so by default when down that hole.

That's normally a bad approach anyway: given a problem and automatically responding with "we're going to use platform X, database Y, web stack Z, etc…" without thinking about and validating these choices may well come back to bite you later on in the project.

As it happened in this case, the hole wasn't that deep! I soon found that you can't simply drop in the nunit libraries and expect it to work. I did go off on a little tangent searching for ways round this, but soon caught myself and took a little step back.

Enter Silverlight Unit Test Application

In Visual Studio 2010, there's an option to add a new project type named "Silverlight Unit Test Application". To start off, I want to make sure I don't have the same problem regarding the AppBootstrapper having the generic type of ShellView instead of ShellViewModel. Add a new class to the test project that looks like this:

[TestClass]
public class AppBootstrapperTest
{
  [TestMethod]
  public void Initial_model_should_be_the_shell()
  {
    var bootstrap = new AppBootstrapper();
    Assert.AreEqual(typeof(ShellViewModel), bootstrap.GetType().BaseType.GetGenericArguments()[0]);
  }
}

As you can see, it's painfully similar to how the NUnit version would have looked. Simply substitute [TestClass] with [TestFixture] and [TestMethod] with [Test] and you'd be about there.

Then simply run the test project. It will launch a Silverlight application in a browser and run the test:

Silverlight Unit Test Result Page

All green, just and we'd expect.

Continuous Integration?

Next I'd like to run this as part of a continuous build......but this can wait until next time.


Comment Guidelines
See the FAQ for details on the full rules and guidelines. No Spam. Write clearly and thoughtfully - no bad language.