Not too long ago I developed a “production” quality add-in, because this was an add-in that would be used in production it was necessary to develop a number of unit tests to help test the add-in code. I thought I’d take a minute to share a few things I learned during this effort.
The first was tool selection, because I come from a Java background I only knew JUnit and didn’t know of any .NET/C# unit test tools. After doing a bit of research I decided to give NUnit a try. NUnit turned out to be a pretty nice tool for unit testing. What I really liked about NUnit was how easy it was to get up and running. All you need to do is create a new NUnit project and reference your assembly that has your test code. At a minimum in your code you just have to add a [TestFixture] attribute to your test class and add a [Test] attribute to your test methods. This allows NUnit to recognize the class as a test class and the methods as test methods. Here is an example unit test class I wrote, please note this is sample code that falls under our Sample Code Legal Disclaimer:
[CoverageExcludeAttribute]
[TestFixture]
public class AddressLookupContextTest
{
[Test]
public void AddressLookupContextCtorTest()
{
ILogger logger = new Log.impl.Log4NetLogger();
AddressLookupContext ctxt = new AddressLookupContext(logger);
Assert.IsNotNull(ctxt.Logger);
Assert.AreSame(logger, ctxt.Logger);
}
[Test]
public void AddressLookupContextConfigurationTest()
{
ILogger logger = new Log.impl.Log4NetLogger();
AddressLookupContext ctxt = new AddressLookupContext(logger);
AddressLookupConfiguration config = new AddressLookupConfiguration();
ctxt.Configuration = config;
Assert.IsNotNull(ctxt.Configuration);
Assert.AreSame(ctxt.Configuration, config);
}
}
The [CoverageExcludeAttribute] is for NCover, another tool I tried out during this effort. NCover will analyze your code and test code and point out areas where you do not have test coverage. NCover also provides a number of other features, but I was primarily interested in test coverage. NCover will work with NUnit, they even have a nice guide to help you get the two programs to work together. NCover will give you a nice set of HTML files that will outline every class and method and show you exactly where you are missing test coverage.
After I had selected the tools I was going to use I set out developing the code and tests for my add-in. I made the choice early on to include my test code with my add-in code. I simply separated the test code by placing the class files in their own ‘Test’ folder. I then compiled everything into a single assembly. If I had to do it all over again I would not take this approach. I would separate the test code out into its own project within the solution. I think this offers a few advantages over my model:
I don’t remember the exact number but I do know I discovered a number of defects during my unit testing efforts. I’d encourage anyone writing production software to get setup with some type of unit test framework and coverage tool. If you use other tools besides NUnit and NCover I’d love to hear about your experiences with them.

Very informative post on mountainbiking.
Thank you for taking the time to post this, but I would like to say that there are a lot of articles around on NUnit and unit testing, but virtually no articles on how to go about unit testing an add-in. This article would be incredibly informative if you covered that topic. Also, I would love to hear any suggestions you may have on how I can unit test my add-in. For instance, I need to ensure that my add-in is returning the window handle for the main application window that this add-in is plugging into. Thanks again.