Mobile Zone is brought to you in partnership with:

Michael Crump is a Microsoft MVP, INETA Community Champion, and an author of several .NET Framework eBooks. He speaks at a variety of conferences and has written dozens of articles on .NET development. He currently works at Telerik with a focus on our XAML control suite.
You can visit his blog at: MichaelCrump.Net or follow him on Twitter at: @mbcrump

Michael is a DZone MVB and is not an employee of DZone and has posted 100 posts at DZone. You can read more from them at their website. View Full User Profile

Setting up Unit Testing in Windows Phone 7 and 8

02.24.2013
| 3352 views |
  • submit to reddit

Introduction

After reading the comments on this video and in forums, there seems to be a lot of confusion about how to setup “Unit Testing” in Windows Phone 7 and 8. It is actually very easy and I’ll show you step-by-step how to setup your first unit test in Windows Phone 7 or 8.

Let’s Get Started

Before we get started the Visual Studio 2012 Update 2 CTP 2 includes a project template for Unit Testing. The only downside is that this applies to WP8 only. The template will not show if you do not have the 8.0 SDK installed.

If you already have a Windows Phone 7 or 8 project that you want to Unit Test, then all you need to do is to create another Windows Phone 7 or 8 Project in the same solution as shown below. This project can be the standard, “Windows Phone App” template. You may want to give it a meaningful name such as UnitTestWP8App, etc.

image

Right click on the Unit Test WP8 App and select “Manage Nuget Packages”; now do a search for wptoolkit as shown below.

image

You’re going to want to install the Windows Phone toolkit and Windows Phone Toolkit Test Framework. Once that is complete, you will have the necessary files to Unit Test your WP8 projects. Not much has changed except a few new references and images have been added for you.

Navigate over to the MainPage.xaml.cs file and add in the following:

// Constructor
public MainPage()
{
    InitializeComponent();
    this.Content = UnitTestSystem.CreateTestPage();           
}   

You will need to fix your using Statements as this uses: Microsoft.Phone.Testing.

If you set this project as your start-up project and deploy it to the emulator, then you will get the following screen:

1

If we hit the play button then we will find that we have no tests to run.

2

Let’s go ahead and add a reference to our real WP8 application by right clicking references and browsing to solution then projects as shown below.

image

Now that we have a reference to our main project, let’s go ahead and add a simple method that we will use with our Unit Test application.

Navigate to your MainPage.xaml.cs in your main project and add the following code snippet under the MainPage constructor.

public static int AddIntegers(int a, int b)
{
    return (a + b);
}

That was real hard wasn’t it? OK, this will simply add two numbers and return the value. (I hope you had that part figured out.)

Let’s return to our Unit Test project and creating a new folder called, “UnitTests” and add a class called “WP8UnitTest”.

Drop in the following code snippet (you may have to change your namespace if you didn’t name this project UnitTestWP8App)

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace UnitTestWP8App.UnitTests
{
    [TestClass]
    public class WP8UnitTest
    {
        [TestMethod]
        [Description("Check to see if MainPage.xaml get instantiated")]
        public void MainPageTest()
        {
            //Should return true
            MyAwesomeWP8App.MainPage MPage=new MyAwesomeWP8App.MainPage();
            Assert.IsNotNull(MPage);
        }

        [TestMethod]
        [Description("Check to see if AddIntegers works as desired")]
        public void AddIntegersTestShouldPass()
        {
            //Should Pass Since we are using Assert.IsTrue and 2+3=5
            var c=MyAwesomeWP8App.MainPage.AddIntegers(2, 3);
            Assert.IsTrue(c == 5);
        }

        [TestMethod]
        [Description("Check to see if AddIntegers works as desired")]
        public void AddIntegersTestShouldPass2()
        {
            //Should Pass since we are using Assert.IsFalse and 2+3 does not equal 7
            var c=MyAwesomeWP8App.MainPage.AddIntegers(2, 3);
            Assert.IsFalse(c == 7);
        }
    }
}

From here, we have 3 TestMethods that are going to run.

  • The First one just checks to see if the MainPage.xaml gets instantiated in our main application.
  • The Second one calls our AddIntegers method and adds two numbers and expect the result to be 5.
  • The Third one calls our AddIntegers method and adds two numbers and expects the result to NOT equal 7.

If we run our Unit Test Application now and hit the play button, then we can see all three tests passed.

3

We can also drill into them and get more details as well as email it or save it to isolated storage.

4

Wrap-up

I hope this helps you understand Unit Testing in WP8. As always, I’m available for any type of help that you may need. You may also download the completed project here.

Published at DZone with permission of Michael Crump, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)