Unit Testing Windows Phone Applications

January 27th 2014 Unit Testing Windows Phone

If you're used to depend on unit testing during your development or even practice test driven development, working on Windows Phone applications can be a challenge. The fact that the code must always run on the phone (or an emulator), poses some limitations on the execution of the tests.

For quite some time the only available option for unit testing Windows Phone applications was the Windows Phone Toolkit Test Framework. It required you to create an additional Windows Phone application containing the unit tests. To run them, you had to actually run this application either on the emulator or on the phone and start them manually through the UI on the phone. Obviously the approach was from perfect. Because of the lengthy manual process for running the tests, you would usually avoid running them too often, diminishing their value a lot. Still, having the possibility to write and run unit tests was better than not having that option at all.

Running WP Unit Tests

WP Unit Test Results

Visual Studio 2012 Update 2 brought built-in support for unit testing Windows Phone 8 applications, making the above described approach obsolete, unless you're working on a Windows Phone 7 application. Now there is a Windows Phone Unit Test app template available in Visual Studio. The unit tests that you put in a project based on this template can be run directly from the Visual Studio test runner, as well as the ReSharper test runner which I personally prefer. The test runner will take care of starting the emulator, deploying the app and running the selected tests on it. The results will be displayed directly in the test runner's interface just like they are for unit tests on other platforms.

Although this makes the situation much better, there is still a minor issue, interfering with my usual development workflow. I got used to NCrunch running the unit tests continuously for me and showing the results directly inside the code editor in the gutter of the lines covered by the tests. Unfortunately NCrunch doesn't yet properly support Windows Phone platform and can't run the tests on the emulator.

Still, there are ways to use it at least for some of the tests. You will need to use portable class libraries to achieve that, though. Putting your app's business logic in a portable class libraries allows you to reference this library from a regular .NET framework Unit Test Project. NCrunch is able to run those flawlessly. Not only that; since a portable class library can be used on any supported platform, it will be much easier to port the app to Windows Store and Windows Desktop; and with the help of Xamarin tools even to Android, iOS and MacOS.

Of course, not all the code can be put inside a portable class library. As soon as it uses any platform specific APIs, it will need to remain in the native Windows Phone part of the app. For most of such code this shouldn't be a problem, since you can always encapsulate it and hide it behind an interface which you want to mock in unit tests, anyway. The only exception to this is the UI specific code: view models and converters. With most MVVM frameworks their base classes depend on platform specific classes and interfaces, making them unsuitable for portable class libraries. If you really want to put those in a portable class library as well, you should take a look at MvvmCross. It's the only MVVM framework I know of, which allows portable view models and converters. Of course, it doesn't make any sense to port your existing project from a different framework to MvvmCross, just to be able to use NCrunch; but if you're starting a new project or planning to support multiple platforms, it is definitely an option worth considering.

Copyright
Creative Commons License