DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Setting up Unit Testing in Windows Phone 7 and 8

Setting up Unit Testing in Windows Phone 7 and 8

Michael Crump user avatar by
Michael Crump
·
Feb. 24, 13 · Interview
Like (0)
Save
Tweet
Share
5.47K Views

Join the DZone community and get the full member experience.

Join For Free

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 .

unit test Windows Phone

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Explainer: Building High Performing Data Product Platform
  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Unlocking the Power of Polymorphism in JavaScript: A Deep Dive
  • Debugging Threads and Asynchronous Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: