Running and debugging unit tests with NUnit and Visual Studio

Posted by on January 5, 2007

For .NET developers a very usefull and free framework for unit testing is NUnit, that is a .NET implementation of the popular JUnit for Java.

I am using this for unit testing zigGIS, the Open Source ArcGis Desktop connector for PostGIS in which I am currently involved.

I will show you in this post how easy and powerfull is to create, run and debug unit test for a .NET library or application.

After installing NUnit, you can add a reference to it in your VS solution. After doing so you are ready to implement one or more classes (fixtures) for performing unit tests.

In my case I wrote this simple class with a setup and two test methods.
The first method is checking if the created layer renderer is a ISimpleRenderer, the second is checking if is a IUniqueValueRenderer.
For more information about NUnit attributes like [TestFixture], [SetUp], [Test] and for using the NUnit framework assertions like Assert.IsInstanceOfType please consult NUnit documentation, it is very easy to get started with it in a while.

[TestFixture]
public class PostGisFeatureClassTester
{
IFeatureLayer layer;
 
[SetUp]
public void Init()
{
// Open workspace and feature class.
IWorkspaceFactory wksf = new PostGisWksFactory();
IFeatureWorkspace fwks = (IFeatureWorkspace)wksf.OpenFromFile(@"C:ziggisZigGisexample.zig", 0);
IFeatureClass fc = fwks.OpenFeatureClass("zone");
// Create the new layer (default renderer is ISimpleRenderer)
layer = new PostGisFeatureLayer();
layer.FeatureClass = fc;
layer.Name = fc.AliasName;
}
 
[Test]
public void RendererIsISimpleRenderer()
{
IGeoFeatureLayer gfl = layer as IGeoFeatureLayer;
IFeatureRenderer fr = gfl.Renderer;
Type expected = typeof(IFeatureRenderer);
System.Console.WriteLine(expected.ToString() + ", " + fr.GetType().ToString());
Assert.IsInstanceOfType(expected, fr, "test ok");
}
 
[Test]
public void RendererIsIUniqueValueRenderer()
{
//from simple (default) to unique value
//TesterUtilities.ApplyUniqueValueRenderer(layer as IGeoFeatureLayer);
IGeoFeatureLayer gfl = layer as IGeoFeatureLayer;
IFeatureRenderer fr = gfl.Renderer;
Type expected = typeof(IUniqueValueRenderer);
System.Console.WriteLine(expected.ToString() + ", " + fr.GetType().ToString());
Assert.IsInstanceOfType(expected, fr, "test ok");
}
}

After building the unit test library I can run the NUnit console and run the unit tests, if they successes they are showed in green, if not they are showed in red (the second fails, as the renderer is not an IUniqueValueRenderer.

NUnitVSDebug1

If we want to debug the unit test we wrote, the best solution I have found so far is to start the NUnit console from visual studio.
That’s pretty easy, what is needed to do is the following:
1) go at the unit test library to be debugged and from project properties at the debug tab check the ‘Start external program’ option and browse for NUint console exe
2) at the ‘Command line arguments’ set the path to a NUnit project that is unit testing the test library.

In my case the NUnit project, a .nunit extension text file that can be easily created from the NUnit console, is the following:



Now start debugging the unit test project in Visual Studio. As soon as debug is started, the NUnit console is also started and tests starts running. You can set now breakpoints, and easily start debugging your tests.
Be sure to have an application entry point in your project, is enough to have a dummy class with a Main static void with the [STAThread] attribute.

You can look for a different approach, with the MSTest framework (that I think is included in the Team Edition of Visual Studio 2005) at this Dave Bouwman’s post.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • LinkedIn
  • Reddit
  • StumbleUpon
1 comment on Running and debugging unit tests with NUnit and Visual Studio

Closed

  1. Louis-Philippe Carig says:

    Hi,

    I have an ArcMap extension written in C#. I would like to test it with NUnit. Unfortunately, since NUnit is running my code, objects in ArcMap are nulls (Application, MxDocument, etc). Do you know how to unit test an ArcMap extension written in C#? I would have to start ArcMap and then run NUnit on top of ArcMap. But I don’t know how.

    Thanks