Home > Articles > Programming

  • Print
  • + Share This
From the author of

How NUnit Works

I must confess that even though I'm writing from an Extreme Programming point of view, NUnit can be used across whatever software development methodology you're using. XP is different in that it embeds testing into the development process, encouraging developers to write their tests before they code. Why not try this approach? So, with my XP hat firmly placed on my sparsely populated head, writing tests with NUnit means taking these steps:

  1. Create the shell or stub class.

  2. Create a test case.

  3. Run the test through the NUnit test runner.

    The test should fail at this point.

  4. Go back to the class and add just enough code for the test to pass.

    Figure 1 below illustrates the typical test cycle you will go through as you write unit tests with NUnit.

    Figure 1Figure 1 The NUnit testing cycle.

Setting Up and Running NUnit

Enough talk; let's start testing with NUnit. You can download the latest version of NUnit from http://nunit.org/. The small size of the download will be a pleasant surprise—unlike the .NET framework, which dimmed the lights on my entire block when I downloaded it. Installing NUnit is as easy as double-clicking on the nunit.msi Windows Installer file.

You should have no problem setting up NUnit, assuming you installed .NET first! NUnit comes with both command-line and GUI test runner tools. In today's session I'll use the GUI version—I like the green bar I get with a passed test.

NUnit comes with a number of simple sample tests in C#, C++, VB and J#; it's a good idea to use these to test your NUnit installation. You'll find the samples under the c:\Program Files\NUnit V2.0\src\samples. Open-up the csharp-sample.csproj under the csharp folder. Note that due to a bug in the NUnit installer you'll need to remove and re-add the nunit.framework.dll reference before you compile the project. Build the project and you're ready to test.

Start the NUnitGUI application (you will find this under your Start menu in the NUnit V2.0 folder) and then select File->Open to the load c:\Program Files\NUnit V2.0\src\samples\csharp\bin\debug\csharp-sample.dll library. Click Run and you should see the screen shown in Figure 2.

Figure 2Figure 2 NUnitGUI application showing results from the test run on csharp-sample.dll.

Great. You've already broken NUnit and have a red bar of disapproval. Actually, this isn't the case because the csharp-sample.dll has some known defects (that explains the nasty red bar). Check the sample code for this DLL under the src folder if you don't believe me.

NUnit is now installed and is ready to test your .NET components.

  • + Share This
  • 🔖 Save To Your Account