Copyright 2004 by Charlie Calvert
You will need to be sure that NUnit is installed. Go to http://www.nunit.org and download their latest release. This is pretty much a forhead install. Just point the Windows Explorer at the download install executable, and hit the return button with your forehead (or use your hands), and everything else should proceed automatically. I always install every programming related tool in a directory that does not have any spaces in it, though in this particular case there is no need to take that step.
This article is primarily for Delphi for .NET. However, the steps are nearly identical when creating a CSharp application. The source code for the CSharp example is included in this document as Listing 2.
After you have installed NUnit, it is time to create a simple application. Begin by choosing File | New | Other and creating a Delphi for .NET library, as shown in Figure 1. Be careful not to create a VCL library.
This may seem like a peculiar first step. NUnit tests, however, are implemented as libraries; that is, as DLLs. These libraries are then hosted either by the console or gui version of an application that ships with NUnit. The GUI application is called nunit-gui.exe and the console application is called nunit-console.exe. Needless to say, the GUI application shows the results of your test in a colorful GUI application, and the console simply shows the results of your tests as text displayed at the command line. When running from inside the IDE, it is usually best to run tests from the GUI. If you are automating tests that are designed to run at regular intervals, such as each morning at 4 am, then it is best to run the console tool, and to write the results to a text file that can be examined later on.

Figure 01: Be sure to create a Delphi for .NET Library by selecting the right category on the left hand side of the New Items dialg.
The next step is to tell the IDE what application will control your test library, and then pass the name of your library as a parameter to that application. You perform this step by first selecting Run | Parameters from the Delphi menu. The dialog shown in Figure 2 appears. Set the host application to the nunit-gui.exe application that ships with NUnit. This application will host your test library. Now pass the location of the library you will create in as a parameter to nunit-gui.exe. Finally, you can pass in a working directory. This later step will probably not be important unless you need to access files on disk.

Figure 02: This is the Parameters dialog in Delphi, accessed by selecting Run | Parameters from the menu. Set the host application to nunit-gui.exe and optionally specify the DLL containing your tests and a working directory.
There is a three step process involved in adding the NUnit assembly to your project as a reference. Essentially, what you are doing is making sure that .NET can find the DLL that holds NUnit.

Figure 03: Adding a reference to a new Delphi project.
Figure 04: First select the DLL in the top half of the dialog. Then press the Add Reference button to list the file in the New References section at the bottom.

Figure 05: If the DLL is not listed in the Add References dialog, then use the Browse button in the Add Reference dialog to bring up this file open dialog and add it to the list of available assemblies.
After registering the nunit.framework.dll binary file in the references section you should be able to see it in the project manager, as shown in Figure 06.

Figure 06: When you are done, the nunit.framework library should appear in the references section of the project manager.
You are now ready to begin writing your test. To review, the steps you needed to perform were:
Leave the autogenerated main file of your library alone. It is simplest to implement your test in one or more additional units.
Select File | New | Unit from the Delphi menu and save your file to disk in the same directory as your main library file. Fill in the unit with the source code shown in Listing 1. Listing 2 shows the same code in CSharp.
Listing 1: A simple unit that contains one, very simple, test.
unit SimpleTest;
interface
uses
NUnit.FrameWork;
type
[TestFixture]
TSimpleTest = class
public
procedure Test01();
end;
implementation
{ TSimpleTest }
[Test]
procedure TSimpleTest.Test01;
begin
Assert.IsTrue(1 = 1);
end;
end.
Listing 2: A simple CSharp unit test that contains one, very simple, test
using System;
using NUnit.Framework;
namespace Project1
{
/// <summary>
/// Simple example of a CSharp Unit Test.
/// </summary>
[TestFixture]
public class SimpleTest
{
public SimpleTest()
{
//
// TODO: Add constructor logic here
//
}
[Test]
public void Test01()
{
Assert.IsTrue(1 == 1);
}
}
}
The code contains a single item in the uses clause that references NUnit.Framework:
uses NUnit.FrameWork;
This code would not compile if you did not add nunit.framework.dll in the references section of your application, as shown in Figure 6.
The next step is to declare the class that will host your test methods. This class should have [TextFixture] specified as an attribute:
[TestFixture] TSimpleTest = class end;
It should contain one or more public methods that contain assertions. The methods should either begin with the word Test or contain the [Test] attribute. For instance, the sample shown here meets both requirements, either one of which would have been sifficient to mark the method as a test:
[Test] procedure TSimpleTest.Test01; begin Assert.IsTrue(1 = 1); end;
The code shown immediately after this paragraph, however, has neither the [Test] attribute nor a method name that begins with the word test. As a result, it will not be marked as a test and will not be reported on by NUnit.
procedure TSimpleTest.MyTest; begin Assert.IsTrue(1 = 1); end;
The screen shot shown in Figure 07 shows what a successful run of this simple test looks like. All the colors shown in this example are green, indicating success. This is a screen shot of the nunit-gui.exe application that ships with nunit. It was launched from inside the Delphi IDE because it was added to the parameter dialog, as shown in Figure 2. After this application was launched, it loaded our custom unit test library, which in this case is called TestLibrary.dll. It contained a single test case, called Test01.

Figure 07: The test library as it appears when run inside the nunit-gui.exe application that ships with NUnit.
This short article was designed to get you up and running with NUnit in Delphi for .NET. Few details were given outside the basic ones needed to ensure that you could set up and run tests successfully. The basic steps involved:
In closing, I should add that unit testing is a simple technology that is part of a more complicated, and extensive philosophy of programming. Even this simple example should give you some sense of how you can use NUnit to test features of your application. However, it is important to do much more reading on this subject so as to understand how to best use tests in a complex application.
This is easy to set up, but if all else fails, you can resort to the hidden emoticon page.