Thursday, January 17, 2008

Unit Testing for .Net Compact Framework is now friendly enough!

When I joined to Microsoft Patterns & Practices Mobile Client Software Factory team a couple of years ago, I discovered test-driven development as a reality, but the first big issue we had to face then, was the lack of unit testing support for the .Net Compact Framework.

Having so many testing frameworks for the full framework, the .Net CF was nobody's land regarding unit testing. Not even Visual Studio 2005, which includes on its Team Suite edition a nicely integrated Unit Testing framework provides support for .Net CF Unit Testing. There was only one option: build our own Unit Testing framework (or at least our test runner), and that was what we called the p&p Compact Framework Test Runner, which is part of the Mobile Client Software Factory.

It was just enough effort to make things work. There was a big wish list for cool features that never were done like IDE integration. I want to mention also another old project with the same mission, the CFNUnitBridge by Trey.

Now Visual Studio 2008 (formerly Orcas) has arrived, and it includes support for .Net CF Unit Testing which very nice IDE-integration, with the same approach of the full framework unit testing. Such a good news!

Wanna try it?

Let's do a remake of a very interesting Daniel Moth's post from a couple of years ago:

1) Let's create a new Smart Device Class Library for .Net CF 3.5 on Visual Studio 2008

2) Add a new class to the project with the following content:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace AppForTest
public class Class1
public Int32 GetBuildVersionPart()
return System.Environment.Version.Build;


3) Right Click on the method and choose "Create Unit Tests..."

And select only the GetBuildVersionPart() method

Enter the Test Project name

It will create a new test project, unit testing class and the following unit test method:

/// <summary>
A test for GetBuildVersionPart
public void GetBuildVersionPartTest()
Class1 target = new Class1(); // TODO: Initialize to an appropriate value
int expected = 0; // TODO: Initialize to an appropriate value
int actual;
actual = target.GetBuildVersionPart();
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");

4) Replace expected = 0 with expected = 50727 which is the build number for the full framework 3.5, and comment out the Assert.Inconclusive line (or delete it).

5) Run the GetBuildVersionPartTest from the Test View window:


It will build the projects, launch the emulator and run the tests on the emulator. After some seconds, the result will be:


This time the test has failed, the expected value was 50727, the build number for the full framework, but the actual value was 7283, the build number for the compact framework.

This time the Unit Test has run on the emulator! It really was a .Net CF Unit Test :)


Jeffry @ CWR Mobility said...

Hi José,

That's indeed great news. I haven't tried it myself though since I'm still on VS2005.

What I'm curious about is how this will work in an automated build environment. Can I launch these unit tests from MSBuild for example? Any ideas about that?

Jose Gallardo said...

Very good point Jeffry. I'm glad to say It's possible. Obviously you'll need .Net CF and Windows Mobile SDKs installed on the build server, and launching MSTest from msbuild will launch the corresponding emulator and deploy/run tests on it. I've already tried it, and after a little tweak it did work. I'll post about it soon. Thanks for your feedback!

Jeffry @ CWR Mobility said...

Excellent, looking forward to it!

Anonymous said...

I tried to run a simple unitTest using the MsTest.exe and it worked fine. The only catch is to use the /runconfig: option and specify the config file so that it knows which emulator to lunch to execute the tests.
I have only one problem which is if I turn on the code coverage the test won't run and I get an System.TypeLoadException. Any idea if the code coverage in VS2008 is supported for mobile devices???

Rick said...

I am looking forward to this post as well. I am currently setting up a build server I will need to get my CF tests to run on it.


Jose Gallardo said...

I'm sorry for the delay. I was at vacations last week.
Post done:

anonymous: Unfortunaly, as far as I know test coverage is not supported for smart devices Unit Testing :( I'll try to find out some workaround because it's a very desirable feature.

Anonymous said...

If you're using vs2005 and want to make unit tests for devices easily. Check out roaster at codeplex.

Peter Douglas said...

Thank you for this. Does anyone have luck with peripheral serial com port cn and testing? My emulator tells me "No Bluetooth installed on this device" when I try to add a BT connection. When I edit the solution's .testrunConfig file and instruct the test harness to use my connected device I suspect it ignores my instruction. Also, any advice on adding break points? My tests never stop on a break.

Anonymous said...

I corrently have a test project for my Application (Mobile) which was not started like this. If I select a method to test from the main project from they main project and create a auto-create unit test case, how do I make sure that it is added to Tests project that I already have?

Anonymous said...

any further info on the code coverage section? We can write unit tests but cannot measure the effectiveness of the unit tests without code coverage functionality.