Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

·
Free Resource

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 ~

Prerequisites

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.

1. Create Silverlight Phone Application

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”.

2. Create Phone Applicatoin for Unit Test

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?

3. Visual Studio 2010 Phone Solution Explorer

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.

Remove  RootVisual from App.xaml File

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.

Delete unnecessary files

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

Jeff MIX website -  Download Silverlight for Phone 7 Unit Test Framework

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”.

6. Add Test Framework as a Reference in Test  Project

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.

8. Add UnitTestSystem.CreateTestPage in App  Constructor

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

Set Unit Test Project as a startup

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.

Simple Test

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.

[TestClass]
public class SampleTest {
[TestMethod]
public void AlwaysPass() {
Assert.IsTrue(true);
}
}

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.

Possiblely  a bug in Unit Test Framework

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.

UnitTest with AlwayPass

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.

Your code looks good

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

[TestClass]
public class SampleTest {
[TestMethod]
public void AlwaysPass() {
Assert.IsTrue(true);
}

[TestMethod]
public void AlwaysFail() {
Assert.IsTrue(false);
}
}

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

Test Failure

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.

Test Failure Details

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!!

 

Topics:

Published at DZone with permission of Michael Sync. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}