×
Namespaces

Variants
Actions

How to create unit test with the Windows Phone Toolkit Test Framework

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to create and execute unit tests for Windows Phone apps.

Tip.pngTip: From Visual Studio 2012 Update 2 (Windows Phone 8) the IDE integrates the Windows Phone Unit Test framework. This makes it easier than the approach outlined in this article, as there is no dependency on the Windows Phone Toolkit (or other third party) project. For more information see:

WP Metro Icon UI.png
WP Metro Icon Bug.png
WP Metro Icon Tools.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 820, Nokia Lumia 800
CompatibilityArticle
Created: mfabiop (09 Jul 2013)
Last edited: hamishwillee (29 Jul 2013)

Contents

Introduction

There are many mechanisms to achieve good software quality. One of the most important is automated testing. Running automatic tests on your code can help you to anticipate potential problems and guarantee that any changes made don't break your code.

Unit testing is the most common and easy mechanism for testing code. It tests small units of the code only, which makes this kind of test very easy to implement and update.

This article explains how you can use the Windows Phone Toolkit Test Framework (from nuget) to add unit tests to your project.

Start up configuration

This article is going to show the needed steps to start using unit tests with a Windows Phone (7.5 or 8) project. As we need a project to test, this article will include the creation of the project under tests. If you want to implement test on your project, begin from step 2.

Step 1 - Creating a project to test

Create a new Windows Phone application project named ProjectToTest and create a class as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ProjectToTest
{
public class Calculator
{
 
public double sum(double a, double b)
{
return a + b;
}
 
public double subtract(double a, double b)
{
return a + b;
}
 
}
}

Step 2 - Creating and configuring the test project

Now, we are going to create the project that will test the project created on step 1.

  • Create a Windows Phone application project named TestProject in the same solution.
  • Right click on the TestProject project and select "Manage NuGet Packages..." option. If you don't have the NuGet installed, it is a good time to install it. It's a very useful package manager for Visual Studio.
    Select-nu-get-manager.png
  • Search for windows phone toolkit test framework on online repository and install both Windows Phone Toolkit and Windows Phone Toolkit Test Framework.
    Install-wpt.png
  • Set the TestProject as the StartUp project of the solution.
  • Put the below code in the MainPage.xaml.cs file. It's needed to create the UI interface of the unit test framework.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using TestProject.Resources;
    using Microsoft.Phone.Testing;
     
    namespace TestProject
    {
    public partial class MainPage : PhoneApplicationPage
    {
    // Constructor
    public MainPage()
    {
    InitializeComponent();
    this.Content = UnitTestSystem.CreateTestPage();
    }
    }
    }
  • Add a reference to the project under test (the ProjectToTest created in step 1 or any other project that you want to test) in the TestProject created in this step.

Step 3 - Writing the tests

Create a folder named UnitTests and a new class inside this folder named CalculatorTest (Please check the attached project for better understanding the layout of the folders). Put the below code in this class.:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectToTest;
 
namespace TestProject.UnitTests
{
[TestClass]
public class CalculatorTest
{
 
private Calculator calc;
 
[TestInitialize]
public void Initialize()
{
calc = new Calculator();
}
 
[TestMethod]
[Description("Test sum method")]
public void TestSum()
{
double a = 5;
double b = 4;
//Verify if the values are equals
Assert.AreEqual<double>(9, calc.sum(a, b));
}
 
[TestMethod]
[Description("Test subtract method")]
public void TestSubtract()
{
double a = 5;
double b = 4;
//Verify if the values are equals
Assert.AreEqual<double>(1, calc.subtract(a, b));
}
 
[TestCleanup]
public void Cleanup()
{
calc = null;
}
 
}
}

Step 4 - Running the tests

Note.pngNote: You have to select the Release configuration in order to run the tests on emulator or device. If you choose Debug, any failed test will break the execution


Run the TestProject on the emulator or device and click on play button.

Unit-test-screen1.png


See the test results. Something was wrong!

Unit-test-screen2.png


For the demonstration purpose, the subtract(double a, double b) method contains a failure test case. It is executing a wrong operation, just fix it and run the test again.

Summary

Some points about the Windows Phone Toolkit test framework.

  • The [TestClass] attribute must be placed before any test class definition.
  • The [TestMethod] attribute must be place before any test method definition.
  • The [TestInitialize] and [TestCleanup] attributes are executed before and after any test method respectively.
  • There are other attributes that you can use in your tests.

Following is another example of a unit test class. I hope this can help.

using MapAlarmWP8.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace MapAlarmTestProject.UnitTests
{
 
[TestClass]
public class DatabaseLibraryTest
{
 
private ApplicationDatabase database = new LocalSQLiteDatabase();
 
//Executed before each test.
[TestInitialize]
public void Initialize()
{
database = new LocalSQLiteDatabase();
}
 
[TestMethod]
[Description("Test saving a place")]
public void SavePlaceTest()
{
//First, try to save a new place
string placeName = "TestName";
string placeTag = "Work Market";
double placeLatitude = 7.32;
double placeLongitude = 35.32;
 
bool creationResult = database.CreatePlace(placeName, placeTag, placeLatitude, placeLongitude);
//Verify if the place was saved.
Assert.AreEqual<bool>(true, creationResult);
 
//Retrieve the saved place
Place place = database.GetPlaceByName(placeName);
//Another implementation of AreEquals accepts a third parameter with the optional message if an error happens
Assert.AreEqual<string>(placeName, place.Name, "The names should be same");
Assert.AreEqual<string>(placeTag, place.Tag, "The tags should be same");
Assert.AreEqual<double>(placeLatitude, place.Latitude, "The latitudes should be same");
Assert.AreEqual<double>(placeLongitude, place.Longitude, "The longitudes should be same");
}
 
//Executed after each test.
[TestCleanup]
public void Cleanup()
{
database = null;
}
 
}
}

References

  1. Windows Phone Toolkit
  2. Unit test
This page was last modified on 29 July 2013, at 06:52.
318 page views in the last 30 days.