Compartir a través de


Unit testing apps for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

This topic contains the following sections.

Unit testing a Windows Phone app

To create and run unit tests for a Windows Phone app, you have to do the following tasks in Visual Studio.

  1. Add a Unit Test App project to the solution that contains your Windows Phone project.

    In the unit test project, add a reference to the Windows Phone project.

  2. In the unit test project, create unit tests.

  3. On the toolbar, select the emulator or the target device on which to run the tests.

    Open Test Explorer. From Test Explorer, run the unit tests.

After you run your tests, fix errors that the tests find in your code and repeat the tests as necessary.

These tasks are described in the following sections.

Setting up the sample Windows Phone project

This section begins a walkthrough of a simple unit test for a Windows Phone app. This walkthrough continues in the following sections. This example is based on the example at Walkthrough: Creating and Running Unit Tests for Managed Code.

To set up the sample Windows Phone project

  1. In an existing Windows Phone project, add a new class named BankAccount.

  2. In the new class file, replace the default code with the following code.

    Later, you’ll create a unit test for the Debit method in this class.

    using System;
    
    namespace Bank
    {
        public class BankAccount
        {
            public string CustomerName { get; private set; }
            public double Balance { get; private set; }
    
            private BankAccount()
            {
            }
    
            public BankAccount(string customerName, double balance)
            {
                CustomerName = customerName;
                Balance = balance;
            }
    
            public void Debit(double amount)
            {
                if (amount > Balance)
                {
                    throw new ArgumentOutOfRangeException("amount");
                }
    
                if (amount < 0)
                {
                    throw new ArgumentOutOfRangeException("amount");
                }
    
                Balance += amount;
            }
        }
    }
    
  3. Build the Windows Phone project to make sure there are no errors.

Adding the unit test project to the solution

Use the Unit Test App template to create a separate project in the same solution as your app project.

To add the unit test project to the solution

  1. In Visual Studio, in Solution Explorer, right-click the solution, and then select Add | New Project.

  2. In the Add New Project window, in the list of Windows Phone templates, select Windows Phone Unit Test App. Name the new project BankAccountTest, and then click OK.

  3. In Solution Explorer, in the new BankAccountTest project, add a reference to the Windows Phone project.

    1. In the new BankAccountTest project, right-click References, and then select Add Reference.

    2. In the Reference Manager window, select Solution.

    3. In the list of projects in the solution, select the box next to the Windows Phone project, and then click OK.

After you add the unit test project and add the reference, the solution looks like this:

Creating a test method

A new unit test project contains a default class file named UnitTest1. This class contains the following default code, which includes a single empty instance of TestMethod.

using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

namespace BankAccountTest
{
    [TestClass]
    public class BankAccountTest
    {
        [TestMethod]
        public void TestMethod1()
        {
        }
    }
}

To create a test method

  1. In Visual Studio, in the unit test project, open UnitTest1.cs.

  2. In the UnitTest1 class file, replace the default code with the following code.

    This code creates a unit test for the Debit method in the BankAccount class that you added to the Windows Phone project. The unit test creates an instance of the BankAccount class, debits the specified amount, and then tests whether the new balance is equal to the expected balance.

    using System;
    using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
    using Bank;
    
    namespace BankAccountTest
    {
        [TestClass]
        public class BankAccountTests
        {
            [TestMethod]
            public void Debit_WithValidAmount_UpdatesBalance()
            {
                // arrange
                double beginningBalance = 11.99;
                double debitAmount = 4.55;
                double expected = 7.44;
    
                BankAccount account = new BankAccount("New Customer", beginningBalance);
    
                // act
                account.Debit(debitAmount);
    
                //assert
                double actual = account.Balance;
                Assert.AreEqual(expected, actual, 0.001, "Account not debited corrected");
            }
        }
    }
    
  3. Build the unit test project to make sure there are no errors.

Running the test in Visual Studio

You typically use Test Explorer in Visual Studio to run the unit tests that you create.

To run the test in Visual Studio

  1. In Visual Studio, on the toolbar, select the emulator or the target device on which to run the tests.

  2. In Visual Studio, if Test Explorer isn’t already open, select TEST | Windows | Test Explorer. The Test Explorer window contains the single test that you created, and looks like this:

  3. In Test Explorer, click Run All.

    The test reports a failure, because there is in fact an error in the code of the Debit method. As a result, the actual balance does not equal the expected balance after debiting the specified amount.

  4. In the Windows Phone project, fix the error in the code of the Debit method. To do this, change Balance += amount; to Balance -= amount;.

  5. Build the Windows Phone project to make sure there are no errors.

  6. In Test Explorer, click Run All again.

    The test reports success. You’ve successfully completed your first unit test for a Windows Phone app.

Additional features for unit testing

Methods for running unit tests in Visual Studio

You can run unit tests in Visual Studio by using one of the following methods. You can also run unit tests at the command prompt as described in the following section. You can’t use F5 or Ctrl-F5 to run a unit test project.

To run unit tests in Visual Studio

  • In Test Explorer, use the commands on the toolbar, such as Run All.

  • In Test Explorer, select a test or tests, then right-click and run the selected tests from the context menu.

  • On the TEST menu, run the tests by using one of the options on the Run or Debug submenu.

  • In the code window of a unit test class, right-click, and then select Run Tests or Debug Tests.

Running unit tests at the command prompt

You can also run unit tests at the command prompt by using the vstest.console app. When you run unit tests at the command prompt, you can use an optional .runsettings file to specify a target device other than the default emulator.

To run unit tests at the command prompt in the default emulator

  • Run the unit test project at the command prompt by using the vstest.console app and providing the name of your XAP file.

    The vstest.console app is typically in the following folder:

    C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow

    Use a command similar to the following example:

    vstest.console D:\Projects\BankAccountTest\Bin\x86\Debug\BankAccountTest_Debug_x86.xap
    

To run unit tests at the command prompt with a .runsettings file

  1. Create and save a .runsettings file. For more info, see Configuring Unit Tests by using a .runsettings File.

  2. In the .runsettings file, add an MSPhoneTest element and a TargetDevice element to specify the target device, as shown in the following example.

    <?xml version="1.0" encoding="utf-8"?>
    <RunSettings>
      . . .
      <MSPhoneTest>
        <TargetDevice>Emulator WVGA</TargetDevice>
      </MSPhoneTest>
    </RunSettings>
    
  3. Build the unit test project.

  4. Run the unit test project at the command prompt by using the vstest.console app and providing the name of your XAP file and the .runsettings file.

    The vstest.console app is typically in the following folder:

    C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow

    Use a command similar to the following example:

    vstest.console D:\Projects\BankAccountTest\Bin\x86\Debug\BankAccountTest_Debug_x86.xap /Settings:D:\Projects\BankAccountTest\BankAccountTest.runsettings
    

To capture unit test results in a Test Results (.trx) file

  1. Add /logger:trx to the command that you run at the command prompt. For example:

    vstest.console D:\Projects\BankAccountTest\Bin\x86\Debug\BankAccountTest_Debug_x86.xap /logger:trx
    
  2. Run the command. The command creates a TestResults folder in the current location, and then creates the Test Results (.trx) file in that folder.

Important Note:

This command requires permission to create the output folder and file. If you run the command from a location where a member of the Users group cannot create a folder, you have to open the Command Prompt window as an Administrator. You have to do this, for example, if you run the command from the location of vstest.console instead of from the project folder.

Alternately, you can specify the location of the TestResults folder in the **.runsettings** file, which is described earlier in this topic. In the **.runsettings** file, add a RunConfiguration element and a ResultsDirectory element to specify the target folder, as shown in the following example.

Organizing and finding tests in Test Explorer

In Test Explorer, you can organize and find tests by using the following features.

To organize and find tests in Test Explorer

  • Click the grouping button to group tests by Class, Duration, Outcome, Traits, or Project.

  • Type a search term in the search box to find a test or tests.

  • To filter the list of tests, select a filter criterion from the drop-down list next to the search box to filter the list of tests.

The following screenshot shows these features of Test Explorer.

Differences between unit testing for Windows Phone apps and Windows Store apps

The following table describes differences between unit testing for Windows Phone apps and unit testing for Windows Store apps.

Feature

Unit testing for Windows Phone apps

Unit testing for Windows Store apps

Target for running tests

Emulator or phone

Tests always run locally.

Feature set

The following features are not supported:

  • UITest methods

  • Asynchronous Assert methods

The listed features that are not supported for Windows Phone apps are supported for Windows Store apps.

Team Build support

Not supported.

Includes support for running unit tests as part of Team Build.

Additional info

For more info about unit testing, see the following article:

The following topics describe unit testing for Windows Store apps, which is similar to unit testing for Windows Phone apps.