Friday, December 11, 2009

Unit Testing With Compact Framework and Visual Studio

Following up my issue with running NUnit tests for the Windows Mobile application, I came across a couple of articles on using the unit testing framework integrated in Visual Studio 2008 which is now supposed to be user friendly.
The process starts with selecting the function name, right-clicking on it and selecting "Create Unit Tests"

I can select the functions I want unit tests to be created for - I'll only choose one for now

I am then prompted for the name of the project where my tests will be created. Visual Studio adds a new project to the solution and this is the code for the test method created for the function I chose.

///A test for CreateDatabase

public void CreateDatabaseTest()
DataBase target = new DataBase(); // TODO: Initialize to an appropriate value
Assert.Inconclusive("A method that does not return a value cannot be verified.");

This is great, except that I want to test for the things I want to test. So, of course, I need to change that. That's probably closer to what I want to test in my method:

public void CheckDatabaseCreation()
DataBase target = new DataBase();
target.SetFileName(@"\Program Files\TestDB\TTrack.sdf");
target.SetConnectionString(@"Data Source=\Program Files\TestDB\TTrack.sdf");
int count = target.RunScalar(target.qryCountUsers);
Assert.AreEqual(count, 1);

This is not so much different from the way tests are created in NUnit. In fact, so far there is no difference at all. Now, to run the test. There is a menu item "Test" in the top menu where I can select Test->Windows->Test View and the "Test View" becomes visible.

There I can see my tests - the auto generated one and the one I added myself.

I can run all tests or select any combination of tests I want to run from the Test View and choose either "Run Selection" or "Debug Selection" (I did not find out yet what the difference is - if I place a breakpoint inside the test method and choose "Debug Selection", the execution does not break at the breakpoint). After the test(s) finished running, I can see the result in the Test Results window.

by . Also posted on my website

No comments: