Test-Driven Development in Windows Phone7 – Part 1: Unit Testing with Silverlight for Phone7

This is the first post of “Test-Driven Development in Windows Phone7” series. I will show you how to use Silverlight Unit Test Framework for Windows Phone 7 and how to run the unit tests in Phone emulator. As it’s an introductory post, I will not talk about TDD or stub or mock in this article. (But I promise those things will come in next post. )

Download ~


Creating Window Phone Application and Unit Test project

First of all, we will create a “Windows Phone Application” (I will call it “AddressBook” ) in Visual Studio 2010. “Silverlight for Windows Phone” supports only C# language so you need to find “Windows Phone Application” project under “Visual C#” node. If you are an VB developer, I’ve to say sorry for you. C# and VB.NET used to be under same boat but I’m not sure what makes Windows Phone team to decide to left all VB.NET developer. Anyway, there are numbers of VB.NET to C# converter on the net so it’s not so difficult for you to find out the similar C# keywords.

Next, we will create another “Windows Phone Application” for unit test under same solution. As the time of writing, there is no “Silverlight Unit Test” project template available to download. But I’m sure that Jeff Wilcox is working very hard to get that template for us. According to his new blog http://jeffatmix.com/, we will probably get Silverlight Toolkit with Unit Test Framework on this April. You can’t use “Class Library” project template too.

Okay. Let’s create “Windows Phone Application” and I will call it “AddressBook.Tests”.

Finally, we got two projects in our solution as below.  One is for our application and another one is for unit tests. So far so good, right?

Converting Windows Phone Application to Unit Test Project

As this is not a unit test project template, we  will have to do a few manual steps to make AddressBook.Tests prject as a unit test project.

First, we will remove RootVisual Xaml tag from App.xaml.

And then, we will delete MainPage.xaml file from unit test project. Silverlight Unit Test Framework already has a TestPage so we don’t need Mainpage.xaml anymore.

Now, we need to download the Test Framework from Jeff’s MIX website. http://jeffatmix.com/

Please create a folder (I will call it “LIB” ) in your project directory and extract the zip file and place two assemblies in that folder.

  • Microsoft.Silverlight.Testing.dll
  • Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll

After that, we will add those assemblies in our unit test project “AddressBook.Tests”.

We removed the RootVisual from App.xaml file earlier. Silverlight can’t run without having RootVisual set so we will have set Unit Test page to the Application RootVisual in App.xaml.cs.

Then, we need to set our Unit Test Project as a startup project in Solution Explorer.

Creating Tests

We have converted Silverlight Phone Application into Unit Test project and configured all required things in that project. So, it’s time to write a test. As we are very new to this Unit Test framework and we haven’t used any test framework on Phone 7 emulator, we will create a very simple test class (I will call it “SimpleTest”) just to see how it works on emulator.

We need to add an attribute called TestClass attribute in our test class “SimpleTest”. We will also create a test called “AlwaysPass” with TestMethod attribute in that class.

public class SampleTest {
public void AlwaysPass() {

And then, let’s build the application and run it. If you are running Phone emulator for the first time, it will take a few mins to get loaded. The following is the first screen that you will see..  Don’t be alert that those are not the tests that you wrote. I think it might be a bug in Test framework. I need to ask Jeff about that and will update my post based on his reply.  Just be cool and wait for a few minutes more.

Then, you will see the Test that you wrote on Phone emulator. The list of Test classes under your project will be shown in first page. You can go into each class to see all test methods by clicking on the name of class.

You will see “your code look good” in status since all tests are passed. You can also see the total number of tests, how many tests are passed and how many are failed in the right side of status bar.

Let’s add another test method called “AlwaysFail” in our SimpleTest class. So, our test class will be as below ~

    public class SampleTest {
        public void AlwaysPass() {

        public void AlwaysFail() {

Let’s run the project again and see how it looks like if we have a failed test.

The Test class that has some failed tests will be displayed in Red color. You will see how many tests are not passed in status bar as well.

That’s all. It’s just an introduction of how to use Unit Test framework in Window Phone 7. We will implement Addressbook Application with TDD in next post. Feel free to let me know if you have any comment or suggestion.

Happy Silverlighting!!

8 Replies to “Test-Driven Development in Windows Phone7 – Part 1: Unit Testing with Silverlight for Phone7”

  1. So can the assumption be made that the Silverlight testing library can be used for testing web based Silverlight applications as well (not just Win7 based apps)?

  2. Well, Silverlight desktop version is a subset of full .NET framework and Silverlight for WP7 is a subset of Compact Framework. So, you will have to reference different assembly for that.

  3. Hi Michael,
    I’m trying to get this running, but when I execute I get an File Not Found Exception:

    Could not find file ‘__ApplicationSettings’.

    Any thoughts?

  4. I didn’t mention, this is thrown when setting the RootVisial in the App.xaml code behind.

  5. Thanks for that, needs a change now though. The app.xml no longer has the root visual for selecting mainpage.xaml. You now have to change WMAppManifest.xml and change;


Leave a Reply