Unit Testing is an important part of the application development life cycle for any platform. Visual Studio users no longer need to depend on 3rd party frameworks for unit tests on Windows Phone. The Windows Phone Unit Test framework delivered in CTP2 of Visual Studio Update 2 provides a consistent experience for developers to author, run and debug tests for Windows Phone 8 platform. This article walks you through the steps to start unit testing on Windows Phone.
Prerequisites
- Visual Studio 2012
- Windows Phone SDK 8.0
- Visual Studio 2012 Update 2 CTP 2
Note: You need a 64 bit Windows 8 physical machine (with Hyper-V enabled) to run unit tests on Windows Phone Emulator.
One Framework – Multiple Platforms
You can now write Unit Tests for Windows Phone in C# by creating a new “Windows Phone Unit Test App” project under Visual C#/Windows Phone. Tests are executed on the target device by installing the Unit Test app on the phone.
Figure 1: New Windows Phone Unit Test Template
Whether you are writing classic unit tests, Windows Store unit tests, or Windows Phone unit tests – Visual Studio automatically adds all the necessary references and you are ready to start writing tests using the familiar MS Test framework. You no longer need to learn multiple frameworks to author unit tests for different platforms.
Figure 2: Author tests using MSTest framework
Running Tests from Visual Studio
The Phone UT framework is designed as an adapter on top of the extensible Visual Studio 2012 unit testing platform (similar to other adapters like nUnit, xUnit). With this, the whole Visual Studio Test Driven Development experience is now extended to Windows Phone. Tests are discovered and displayed in the familiar Test Explorer window. Right from within the IDE, users can group/filter tests using traits, turn on continuous test runner to view test results after each build, and debug or run selected (or failed) set of tests.
Figure 3: Phone Unit Tests discovered | Figure 4:Set Target Device |
Visual Studio uses the target device set on the Run palette to install the unit test app, run tests, and return results. Supported targets include Emulators (only available when Hyper-V is enabled) and Windows Phone 8 devices physically connected to the machine. Developers can author, run, and debug tests on any of the target devices without leaving the IDE. While unit tests are running on the device, a 'Tests Running' screen will be shown.
Figure 5: Unit Tests running on device
Running Tests from Command Line
As I mentioned earlier, since Phone UT is designed as an adapter to unit testing platform, you can use vstest.console.exe to execute unit tests from command line. You can also choose to export the results to Visual Studio Test Results File using /Logger:trx.
Figure 6: Run tests from command line
Figure 7: Test Results from TRX logger
By default Emulator WVGA 512MB is the target device to execute tests, use run settings to change target device (Sample run settings attached).
Figure 8: Run Settings to set target device
FAQs and Known Issues
- Phone Unit Test does not support data collectors either on the physical device or emulator.
- Can I write unit tests in native C++?
In CTP2 only C# is supported, but do watch out this space for native C++ support in upcoming releases. (VB# is also not supported) - Which versions of Windows Phone is supported?
Only Windows Phone 8 is supported. Windows Phone 7, 7.5, 7.8 are not supported - Which Visual Studio SKUs do I need to enable this feature?
Windows Phone Unit Tests works on all Express, Test Professional, Professional and Ultimate - After installing all the prerequisites, I still do not see Windows Phone Unit Test template
Workaround: Install the mentioned prerequisites in the specified order, all on the system drive - Device was disconnected or interrupted while unit tests were in progress, what do I do?
Workaround: When a device is disconnected or interrupted (for example an incoming call or camera button activated) test run fails and vstest may crash. Manually uninstall all the unit test applications, connect the device and try running tests again. - I get an error message while debugging unit tests.
Workaround: Manually stop the debugging session at the end of a test run and start debugging selected tests again. - Phone Unit Tests with native dependency cannot be run from command line
- After Test Runs are complete, I sometimes still see Unit Test applications installed on device
Workaround: Manually uninstall all the unit test applications - Known Issue: Users who have a non English Windows Phone SDK 8.0, will not be able to create new Windows Phone Unit Test projects.
- Known Issue: When a test fails, stack trace information does contain line number where test failed and users are not redirected to the appropriate line.