Testing is an essential discipline in developing code, not only to catch and correct outright mistakes, but also to check how that code behaves thoroughly and systematically. Ideally, each individual block of code should be tested by itself to make sure it works as expected, handles boundary conditions properly, and given particular sets of input produces expected and correct output. That's where NUnit comes into play.
NUnit is a unit-testing framework for all .NET languages available through the sourceFORGE.net project. Originally devised as a unit testing environment for Java called JUnit, NUnit represents a port to the .NET environment. It's a text console based environment that works with Windows 32 environments that based on the copyleft licensing scheme developed for the GNU project (in fact, NUnit is part of the GNU Tools for Win32 that falls within the general program). NUnit supports C#, C++ and Visual Basic and is designed to work within the general .NET and Visual Studio environment.
By visiting the NUnit homepage you can download either built classes or source code for NUnit. Those working with stable, released versions of Windows (such as Windows 2000 or XP) can probably use the built classes; those working with RC2 of Windows Server 2003 will want to grab source, because some tweaking will be necessary to make it run (details appear in Bill Wagner's story "Test .NET Code Automatically with NUnit").
Once you've built or installed NUnit, you can run it with the sample tests it includes by running the NUnitGUI project to load its tests and selecting the Browse button to load one of the assemblies provided. You can try out the samples by selecting the SampleMoney.dll assembly to load it, then using the TypeName box to determine which types of assemblies have defined tests. By clicking the Run button, you can execute all defined tests and observe the results in the bottom of the NUnit console window.
Basically, NUnit uses reflection to find test methods within any unit test code you create and identify as such. To create a test suite, create a new class to execute your tests. This new class must derive from the TestCase class, which is part of the basic NUnit environment. Also, any test method in that new class should contain a void return type and take no parameters.
Within NUnit, the Assert method tests the output of a test, where strings may be used to specify failures for individual test elements. Because some test suites require setup or shutdown code before they execute, the NUnit TestCase class provides virtual functions for such activities. Thus, for example, the SetUp method may be used to initialize counters, establish values, and so forth.
To make NUnit integrate properly with TestCase derived classes, they must also include a static method that returns an ITest interface. NUnit provides a constructor that handles this in a single statement. For the complete source code to a sample test class, see Wagner's NUnit Tests page. Working with NUnit is easy, relatively straightforward, and should help make unit testing simpler, faster and easier.
Ed Tittel is a principal at LANWrights, Inc. a wholly-owned subsidiary of iLearning.com, where he writes and teaches on a variety of subjects, including markup languages, development tools, and IT certifications. Contact Ed via e-mail at email@example.com.