Unit Testing Smart Device Projects

Standard

Screenshot of the Smart Client Software Factory's Gui Test Runner

A way of doing Test Driven Development on the Windows Mobile platform in the early days was not particularly easy.

For the development of Webby I created my own NUnit clone that worked inside the exe in debug mode.  This worked really well, but it was limited and I would have preferred something that was integrated into the IDE.

The developers of the “Mobile Client Software Factory” at Microsoft Patterns and Practices felt the need to create a unit testing environment that would enable full “test first” development.  Recently I had the need to investigate their solution, and have found very good.

The factory provides comprehensive unit tests for the application blocks that come with the factory, but more interestingly it provides a way for you to use the same testing framework.

This article below gives an outline of how tests are structured and how to do it.  

The structure of the tests

The test classes utilise the “Visual Studio Team Suite” attribute test markup ([TestClass], [TestMethod] etc).

These test classes are used by two test projects (assemblies):

  • A desktop test project for Visual Studio 2005 Team Suite
  • A mobile class library for running the unit tests on a mobile device

Both the desktop and mobile projects reside in the same project folder (with different names)

Creating the tests

The following procedures describe how you should create and run unit tests
To create and run unit tests on the desktop

  1. Create a Visual Studio 2005 Team System Edition test project that will run unit tests on the desktop.
  2. Add the <TestClass>, <TestMethod>, <TestInitialize>, and <TestCleanup> attributes described earlier to your classes to indicate which are test classes, test methods, initialization methods, and cleanup methods.  (TIP: Reference the application exe or dll as a “File Reference”)
  3. Run these unit tests using Visual Studio 2005 Team System or the TestDriven.NET test runner until all the tests pass.

Next, you can adapt these tests to run on the mobile device. The following procedure describes how you can use the GuiTestRunner utility to run your unit tests in the device emulator or on a mobile device.

To run unit tests in the emulator or on a device

  1. Add the following projects to your application project:
    • GuiTestRunner.csproj from the Tools\CFUnitTester\GuiTestRunner folder
    • TestRunner.csproj from the Tools\CFUnitTester\TestRunner folder
    • UnitTesting.csproj from the Tools\CFUnitTester\UnitTesting folder
  2. If you want to use the test utilities in your tests, also add the TestUtilities.csproj project from the Tools\TestUtilities folder. This project includes utilities to help you read application resources, access the application runtime folder, and provides stopwatches for measuring performance.
  3. Click Add on the File menu, and then click New Project. In the left-side tree view of the Add New Project dialog box, click Windows Mobile 5.0 Pocket PC. In the right-side window of this dialog box, click Class Library. Click OK to add the new project to your existing solution. Use the naming convention for test projects “[Module to be tested].Tests.CF“.
  4. Close Visual Studio and, in Windows Explorer, move your new Class Library project into same folder as desktop test project. This allows the device test project to share the same files as the desktop test project more easily.
  5. Re-open Visual Studio, and open your application solution. In Solution Explorer, right-click the References entry for your application project, and then click AddReference. On the Projects tab of the AddReference dialog box, select your test project, and click OK.
  6. With the device test project selected in Solution Explorer, click Show AllFiles on the Project menu. This reveals all files and folders in the same folder as the desktop test project.
  7. Select all the test and supporting files in Solution Explorer, right-click, and then click Include in Project.
  8. Add the compiler directive to select the appropriate test class namespace at compile time.

    #if PocketPC
     Imports Microsoft.Practices.Mobile.TestTools.UnitTesting
    #else
     Imports Microsoft.VisualStudio.TestTools.UnitTesting
    #endif

  9. In Solution Explorer, right-click your test project, and then click Properties. On the Properties page, click the Devices tab. Change the value for the Output file folder in the Deployment Options section to %CSIDL_PROGRAM_FILES%\GuiTestRunner to ensure that your tests deploy to the same folder as the as the GuiTestRunner utility.
  10. Deploy and execute the GuiTestRunner utility

Links

Advertisements

6 responses »

  1. Pingback: A reference "to that project" could not be added « An Original Idea

  2. Pingback: Write Code Once For Both Mobile And Desktop Apps « An Original Idea

  3. Hi,

    I am developing a smart device (CF 2.0) application with Sql CE 2005 & using VSTS to generate the test scripts.

    I have added the GuiTestRunner, TestRunner and UnitTesting to my device test project (I am using only one test project) and generated the test scripts. My test project does not built as throws the following error :

    Reference required to assembly ‘System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac, Retargetable=Yes’ containing the type ‘System.Data.DataSet’. Add one to your project.

    I believe this error is because of VSTS has internal reference to .Net CF while smart device test project as reference to the .NET CF.

    How do I make use of Mobile Client Software Factory to overcome this error? Also do we need at add the reference of the GuiTestRunner, TestRunner and UnitTesting to my solution or to my device test project? Pl. also provide information on invoking the test scripts whenever the solution is executes.

    Kindly suggest. Thanking you in advance.

    Pl. revert in case of any additional information required.

    Regards
    Anup

  4. As outlined in the post, the trick is to ensure that the desktop test projects don’t refer to compact framework assemblies. The idea is “shared source” between different project types.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s