Unit testing is the process of exercising individual business objects, state, and validation for your applications. A comprehensive set of unit tests written before you develop the code, and executed regularly during the entire development cycle, help to ensure that individual components and services are performing correctly and providing valid results.
You can use a special version of the Silverlight Unit Testing Framework adapted for use with Silverlight 3 to run unit tests for Windows Phone 7 applications. You can obtain this from Jeff Wilcox’s site (http://www.jeff.wilcox.name/2010/05/sl3-utf-bits/). Jeff is a Senior Software Development Engineer at Microsoft on the Silverlight for Windows Phone team.
The Silverlight Unit Testing Framework adds the Silverlight Unit Test Application templates to the New Project dialog box in Visual Studio and Visual Studio Express, and you can use this to add a test project to your application. The framework allows you to execute tests that carry the standard Visual Studio test attributes and include the standard test assertions such as IsTrue and IsNotNull. It can be used to run all the unit tests in a solution or selected tests; it reports the results on the phone, showing full details of each test that passed or failed.
You can also run tests on the desktop using a traditional test framework such as the Microsoft Test Framework (MSTest), instead of deploying the application to the phone or the emulator and running it there under the test framework. This is generally faster, but you must be aware that there are some differences in the execution environment, and tests may behave differently on the desktop when compared to running on the emulator or a physical device. When performing integration testing, you should run the tests on the emulator or a physical device.
There are some tests that you cannot run in an emulator. For example, you cannot test the Global Positioning System (GPS) or the accelerometer. In these cases, you may prefer to use alternative testing techniques, such as creating mock objects that represent the actual service or component that your application uses and substituting these for the physical service or component.
To view a video presentation about unit testing using the Silverlight Unit Testing Framework, see Unit Testing Silverlight and Windows Phone Applications on the Mix 10 website (http://live.visitmix.com/MIX10/Sessions/CL59).
Automated Unit Testing
It is possible to automate testing on the Windows Phone 7 emulator if you need to implement a continuous integration process and build Windows Phone 7 projects on a separate build server that does not have the Windows Phone Developer Tools installed. This requires setting up a folder for the external dependencies and editing the project files. For more information, see the post, “Windows Phone 7 Continuous Integration,” on Justin Angel’s blog (http://justinangel.net/#B logPost=TFS2010WP7ContinuousIntegration).
It is also possible to automate building and deploying an application to a phone or emulator without using Visual Studio. This approach uses the Smart Device Connectivity API implemented in the assembly Microsoft.SmartDevice.Connectivity.dll. For more information about using this API, see “Smart Device Connectivity API
Reference” on MSDN (http://msdn.microsoft.com/en-us/library/bb545992(VS.90).aspx) and “Windows Phone 7 Emulator Automation” on Justin Angel’s blog (http://justinangel.net/#BlogPost=WindowsPhone7EmulatorAutomation).