This article describes the setup instructions for configuring an environment to run isolated unit tests that are intended to run quicky, easily and often to enable test driven development. For integration tests that access isolated storage, web services or phone features, see the WP7-CI project.

This configuration works with all features of the Silverlight Unit Testing framework, including asynchronous tests.

NOTE: These instructions describe the setup for unit MSTest. Why? I personally prefer NUnit, but I use MSTest for my integration tests because of its support for asynchronous Silverlight tests and my preference for a consistant test framework trumps my preference for NUnit.

Before continuing, it is assumed that you have created a Windows Phone application.

Adding a test project

  1. Add a "Silverlight Class Library" project to your solution and select "Silverlight 4" as the version
  2. Add a reference to your Windows Phone project
  3. Add a reference to System.Xml and System.Windows.Browser
  4. Add a reference to Microsoft.Silverlight.Testing and Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight from the Silverlight Toolkit (not the WP7 version)

Referencing Windows Phone assemblies

Since this is a Silverlight 4 project, accessing Phone assemblies should be avoided. However, some assemblies, like Microsoft.Phone.Reactive, may be necessary.

Since the assemblies won't be in the "Target Framework Directory" (Silverlight 4 in this project), you have two choices on how you reference them:

  1. Copy the assemblies to your project from %programfiles%\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone 1 and reference them from there
  2. Add the following snippet to your tests project file after the <ProjectExtensions> that contains <SilverlightProjectProperties> and reference the assemblies from their original location.
<PropertyGroup>
  <WindowsPhoneFrameworkDirectory Condition=" '$(WindowsPhoneFrameworkDirectory)' == '' ">
    $(FrameworkPathOverride)\Profile\WindowsPhone
  </WindowsPhoneFrameworkDirectory>
  <ResolveAssemblyReferencesDependsOn>
    $(ResolveAssemblyReferencesDependsOn);AppendWindowsPhoneAssemblyPath
  </ResolveAssemblyReferencesDependsOn>
</PropertyGroup>
<Target Name="AppendWindowsPhoneAssemblyPath">
  <PropertyGroup>
    <AssemblySearchPaths>
      $(AssemblySearchPaths);$(WindowsPhoneFrameworkDirectory)
    </AssemblySearchPaths>
  </PropertyGroup>
</Target>

Either way, you must make sure Copy Local is set to true for any Windows Phone specific assemblies

1 For Mango, use %programfiles%\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71

Adding a simple test

For the sake of having a test to run, let's create a simple one now.

Create a class called "SampleTest":

using Microsoft.VisualStudio.TestTools.UnitTesting;

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

Running the tests from Visual Studio

  • Visual Studio Test: Silverlight tests are not supported by the built in test runner
  • TestDriven.NET: Run as Silverlight tests
  • ReSharper: Install the AgUnit plugin (you might need to disable the MSTest plugin for AgUnit to work correctly)

Running the tests from the command line (or CI server)

Use the StatLight command line utility:

statlight.exe -d="path\to\assembly.dll" -o=MSTest -v=Feb2011 -r="path\to\log.xml"

By default, StatLight outputs the results in its own format. Alternatively, you can: