concept NUnit in category .net

appears as: NUnit
Continuous Integration in .NET

This is an excerpt from Manning's book Continuous Integration in .NET.

The last essential tool you need is a unit testing tool. The two most common options are MSTest and NUnit (see figure 1.6), but there are others such as MbUnit and xUnit.net. These tools run the unit tests that you write for your application and then generate the results into a text file. The text file can be picked up by your CI server software; a red/green condition for fail/succeed is reported to the team through the feedback mechanism.

Figure 1.6. NUnit runs unit tests on your code and reports the results as red/green for failure or success.

Although NUnit has a GUI tool, it can also be run as a console application as part of your CI process. Many of the tools we’ll discuss in this book have both a GUI and a command-line version. The command-line tools provide results as text or XML files that can be processed by your CI server software; the results are displayed using the feedback mechanism.

In chapter 3, you chose MSBuild as your build engine of choice. You need NUnit to run from within the MSBuild script. There’s an MSBuild Community Task (see chapter 3) to run NUnit that you can use, but you’ll now execute it using the exec task as follows.

Listing 6.3. MSBuild script running the NUnit tests

As you can see, you start the Test target that uses the exec task to execute the nunitconsole.exe application, providing it with the property that contains the DLL to test. The /xml parameter tells NUnit to create an XML report file. You’ll use this file on the CI server to integrate the test results within the feedback mechanism.

6.2.2. Testing with NUnit

NUnit (www.nunit.com) is a legend in the unit testing world of .NET. It’s one of the oldest automated testing frameworks for .NET and was originally a clone of the Java test library JUnit. NUnit has the responsibility of running unit tests and providing feedback about which tests pass and which ones fail. You’ll see that NUnit is easy to use. The easiest way to install it is to download the zip file and extract the core of the testing framework from NUnit-Version\bin\net-2.0\framework into your tools directory.

Listing 6.2. A simple unit test for the rate calculation
using NUnit.Framework; using CiDotNet.Calc.Math; namespace CiDotNet.Calc.Test.Math { [TestFixture] public class FinanceTestFixture { [Test] public void CalculateRate() { int Duration = 12; int Ppy = 12; double PeriodicInterestRate = 7.5; double presentValue = 30000; double finalValue = 0; CiDotNet.Calc.Math.Finance.Mode mode = CiDotNet.Calc.Math.Finance.Mode.BeginMode; double ExpectedRate = 2586.556528260553; double ActualRate = Finance.CalculateRate(Duration, Ppy, PeriodicInterestRate, presentValue, finalValue, mode); Assert.AreEqual(ExpectedRate, ActualRate); } } }

NUnit uses reflection to dig the test methods from the test library. It uses attributes to find what it needs. First, you need to decorate the test class with the [TestFixture] attribute, which tells NUnit that it’s found a class containing tests. All the test methods must be public voids and have the [Test] attribute. In the test code, you can do everything that’s possible in .NET. In the CalculateRate() method, you name the calculation parameters in local variables and fill them with values. You then define the ExpectedRate variable and assign it the value that you expect to be returned from the calculation. The ActualRate variable will be set with the actual calculation value from the Finance library.