Unit testing .Net Compact Framework applications with Visual Studio 2008
Visual Studio 2008 marked an improvement in this as in many other areas: the basic testing tools are included with Visual Studio 2008 Professional, and this time the .NET Compact Framework is supported.
So what do Windows Mobile developers get and how do they go about using it? I thought you’d never ask (and I’d be surprised if you did, since I’m sitting here by myself).
I do want to get one thing out of the way: before anyone says “You’re not doing TDD” I would like to say, pre-emptively, “No, I’m not”. I’m talking about using the tools, not any particular approach to how you go about doing development. That’s up to you (and while you’re about it don’t let the Rails kids bully you).
So anyway, for demonstration purposes I’ve created a mobile application for calculating the Gregorian Easter. The solution contains a class library that implements the algorithm for calculating Easter (based on an example given by Claus Tøndering, in turn based partly on Oudin’s algorithm) and another containing the UI.
The calculation of Easter is a handy example partly because it’s currently on the way to being topical (as you’ll see), and partly because there are plenty of ways of introducing errors which we can use to illustrate the tools.
So here’s my solution in its initial glory and simplicity:

The process of adding a unit test project for the Compact Framework will be familiar to anyone who’s used the desktop version of the tools. In this case I’m creating tests for existing code so I bring up a context menu within the code window and select “Create Unit Tests”:

After a while you’ll be presented with a dialog that allows you to select which methods and classes to test.

(I should apologise at this point for having a class file called EasterCalculator.cs while the class itself is called GregorianEasterCalculator – normally I keep the two strictly aligned, but I mucked up the editing. Which takes a bit of doing).
When you create unit tests this way any method where the cursor was positioned when you right-clicked will be selected by default, but obviously you can deselect this one and/or select others. You can repeat this process at any time, but you should bear in mind that right-clicking to bring up the dialog again and then de-selecting the selected method will not cause an existing test method to be removed (while leaving it selected and continuing will cause an additional test method to be generated for that method).
And here’s my test method as it is generated:

Since the first thing I want to test is that the code works for expected values (you need to deal with the unexpected ones as well of course but we want to establish whether it works as intended) I will enter a known year value (in this case 2009), add checks for values and remove the Assert.Inconclusive(..). I will also rename the method to reflect what I am actually testing (normally naming it with an actual data value would be overly specific but I think this example is a special case):

You should note that while the user interface gives no indication of this, the normal options for debugging unit tests have no effect for smart device projects because the code executes remotely. Debugging smart device tests requires following the steps described here.
Tests can be executed from the Test View (available for instance via Test->Windows->Test View), and results are displayed in the Test Results window also in the same way as for desktop projects.
The following is the result of running a test where Ash Wednesday is being incorrectly calculated as 40 days before Easter Sunday (it should be 46):

Limitations
Perhaps the most serious limitation (as I indicated above when talking about debugging) is that Visual Studio 2008 gives no real indication of what the limitations are, because the UI displayed for testing is the Visual Studio testing user interface. This is perfectly understandable of course, but it does mean some time can be wasted finding out what you can and can’t do.
The form displayed for .testrunconfig files for instance shows Code Coverage as an option, but this is not in fact available for smart devices and will not do anything. To be fair, the form in question shows options that could be relevant for any test run configuration file, so the issue of displaying irrelevant or unavailable options is not actually a problem with the smart device implementation as such.
Conclusion
In a sense the hardest part about demonstrating smart device unit testing support in Visual Studio 2008 is that for anyone familiar with the desktop version there’s nothing new: apart from the fact that this is a special case of executing a remote test, there’s really nothing that’s done differently – it’s more a matter of bearing in mind the features such as Code Coverage that aren’t available, and remembering what you have to do to enable debugging.
The question may arise as to what you can do to make mobile applications more easily testable.
I’ll annoy a few people when I say this, but I wouldn’t recommend going overboard with dependency injection in a mobile application: you don’t really have the luxury of being able to afford to execute code that strictly speaking doesn’t need to be there (on the other hand the way I’ve split my application into separate projects imposes an assembly loading penalty, but I only did that for clarity in my examples).
Some things that are helpful: I personally find the Model-View-Presenter pattern a good fit for the Compact Framework, and Presenter classes are easily testable without mucking around with UI elements. Alex Yakhnin’s MVC framework for the Compact Framework is well worth a look as well (in fact I might switch to it once I’ve had time to try it out).
On the whole though this is definitely not rocket science, so try it out and see what works for you (which is really the only criterion that matters).
But there I’m straying slightly off topic, which means it must be time to stop. So I will.
Download Visual Studio 2008 90 day trial
For detailed information and to request a free 90-day trial DVD of Visual Studio 2008 Team Suite to be sent out to you, go to the Visual Studio webpage.
About the Author
Kevin Daly has been programming professionally for almost 22 years. He has been using .NET and C# continuously since the Beta 2 in 2001, which probably explains the bags under the eyes. Other examples of his views, rants and even the odd code sample can be found on his blog at http://kevindaly.spaces.live.com/blog. He will work for money. He will now stop talking about himself in the third person because it’s frankly a bit weird.
Other related posts:
Writing your own Html Helpers for the ASP.NET MVC Framework
Automating Visual Studio 2008
Windows Azure Table Storage - Getting Started - Part 2
Comment by Dipanwaya, on 6-OCT-2009 04:01
Steps Behind the Test in the VS 2008?
How to test a module in the VS 2008 Environment?
Any Suggestion. (Doc Or PDF)
Add a comment
Please note: comments that are inappropriate or promotional in nature will be deleted.
E-mail addresses are not displayed, but you must enter a valid e-mail address to confirm your comments.
Are you a registered Geekzone user? Login to have the fields below automatically filled in for you and to enable links in comments.
If you have (or qualify to have) a Geekzone Blog then your comment will be automatically confirmed and shown in this blog post.

Comment by PS, on 18-FEB-2009 22:17
it is really great........