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
- Create a Visual Studio 2005 Team System Edition test project that will run unit tests on the desktop.
- 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”)
- 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
- 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
- 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.
- 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“.
- 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.
- 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.
- 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.
- Select all the test and supporting files in Solution Explorer, right-click, and then click Include in Project.
- Add the compiler directive to select the appropriate test class namespace at compile time.
- 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.
- Deploy and execute the GuiTestRunner utility