Migrating Unit Tests from WP Toolkit Test Framework to WP Unit Test App

February 3rd 2014 Unit Testing Windows Phone

Since Visual Studio 2012 Update 2 there is a project template available for unit testing Windows Phone apps: Windows Phone Unit Test App. Unlike its predecessor Windows Phone Toolkit Test Framework, it doesn't require the tests to be manually started from the device or the emulator. They can be started directly from the unit test runner's window in Visual Studio. This feature should be a good enough reason for migrating any existing test projects from the old framework to the new template. I've recently done this with one of my projects and decided to document the process in case I need to it again in the future. I'm posting it here since it might be useful for others, as well.

  1. Rename the existing unit test project, e.g. by adding a .Old postfix. First rename it in Visual Studio to rename the .csproj file, then remove it from the solution, rename the folder in the file system and add back the existing project from the new folder.
  2. Add a new project to the solution based on the Windows Phone Unit Test App template, using the original name.
  3. In the file system copy the files with test and any helper classes to the new project folder.
  4. In Visual Studio Show all files in Solution Explorer and include all the copied files in the project.
  5. Add all the references (except Windows Phone Toolkit Test Framework and Windows Phone Toolkit NuGet packages) from the old unit test project to the new unit test project (NuGet packages, assembly references and project references within the solution).
  6. In the using directives in all test classes replace Microsoft.VisualStudio.TestTools.UnitTesting namespace with Microsoft.VisualStudio.TestPlatform.UnitTestFramework namespace.
  7. Delete any Description attributes on the test methods.
  8. The new unit test project doesn't include AnyCPU configuration; therefore if your solution is configured for AnyCPU, you'll need to open Configuration Manager and enable build for the test project in AnyCPU solution platform. Otherwise the test project won't build and the test won't run, unless you manually trigger the project to build.
  9. If you're using NCrunch, open its configuration window, select the new unit test project and enable the Ignore this component completely option to prevent NCrunch from trying to run the Windows Phone assemblies in its own runtime. It will just cause false failures since it can't run the tests in the Windows Phone runtime.
  10. Now you're ready to compile the project and run the tests.
  11. Of course, the last remaining step is to remove the old unit test project from the solution and delete it.

After I've gone through the above described migration process, three test were failing in the new project. After a more detailed investigation, it turned out they were failing in the old project as well; they just didn't show as such because they were asynchronous (returning async Task). The old framework didn't really support asynchronous tests. Fortunately, the new one does. Another reason for migration, if you ask me.

Copyright
Creative Commons License