Monthly Archives: March 2005

A Month with NUnit

I’d been hearing about this thing called NUnit for the past 6 months, and finally two months or so ago I decided to see what all the hoopla is about.

NUnit is a .Net version of the original JUnit unit-testing package for Java. The newest version has a very strong .Net feel with its use of attributes to control testing.

I decided to start using NUnit in my BRayTracer project. Immediately, I was faced with some issues (not problems, just things I needed to decide).

The first is that NUnit works by creating classes that contain methods which have the TestAttribute applied to them. This allows NUnit to find all of the testing code given a module or project.

I could either put this testing code into the same module as the existing code, or I could break my ray tracing code into a separate module. I didn’t want the testing code to bulk up my executable size (which can in some instances translate to slower code due to paging and locality issues). I decided to break my ray tracing code out into a separate module (I was planning on doing this eventually anyway).

Done with the housekeeping, I began setting up my tests.

So far, I’ve implemented about 150 unit tests covering a few my objects. It’s not totally comprehensive yet, but it’s a good start. Effectively testing the entire rendering process will require thousands of tests.

So far, the tests have helped me fix about 10 serious bugs that I had otherwise missed. The regressions testing is helping a lot, since the code is still in a lot of flux.

I’ve been making a strong effort to write all (most) the tests before I write the code they’re supposed to verify. This definitely makes testing more enjoyable, and more likely to get done.

I’ve been thinking about how to use inheritance and polymorphism to my advantage in these unit tests. I have a Shape object and numerous subclasses (Box, Sphere, etc.). I initially had a few abstract functions in Shape to test basic functionality of all shapes, but I found that this isn’t really buying me
much. So much code has to be taylored to the individual classes that inheritance is helping in that way. More analysis will be needed.

Another aspect of using NUnit is that I’m finding myself needing the debugger less and less. It’s now possible to narrow down problems very quickly with such focused tests. This translates to quicker development.

So far, so good… I’m converted.