RAD Studio (Common)
ContentsIndex
PreviousUpNext
NUnit Overview

NUnit is an open-source unit test framework based on the JUnit test framework. The NUnit framework allows you to develop and execute tests against .NET Framework applications. The RAD Studio integration of NUnit allows you to develop and execute tests for both Delphi for .NET and C# applications. The NUnit framework is not supported in C++Builder or Delphi for Win32. 

This topic includes information about:

  • Developing NUnit Tests.
  • NUnit Asserts.
  • NUnit Test Runners.

Each testing framework provides its own set of methods for testing conditions. The methods support common assertions. You can also create your own custom assertions. You will be able to use the provided methods to test a large number of conditions.  

If you want to create tests for an application, you first create a test project. The test project contains the test case files, which contain one or more tests. A test case is analogous to a class. Each test is analogous to a method. Typically, you might create one test for each method in your application. You can test each method in your application classes to make sure that the method performs the task you expect. 

You can use the Test Project Wizard to generate a test project file, which builds a .NET assembly that must be run by one of the NUnit test runners. You can then use the Test Case Wizard to generates a skeleton test method for each method in the class being tested. This includes local variable declarations for each of the parameters to the method being called. You will need to write the code required to set up the parameters for the call, and the code to verify the return values or other state that is appropriate following the call.  

The following example shows a small C# program that performs simple addition and subtraction:

using System;

namespace CSharpCalcLib
{
    /// <summary>
    /// Simple Calculator Library
    /// </summary>
    public class Calc
    {
        public int Add(int x, int y)
        {
            return x + y;
        }

        public int Sub(int x, int y)
        {
            return x + y;
        }
    }
}

The following example shows the test case skeleton file that you need to modify to test the two methods, Add and Sub, in the preceding code.

namespace TestCalc
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using NUnit.Framework;
using CSharpCalcLib;


// Test methods for class Calc
[TestFixture]
public class TestCalc
{

private Calc aCalc;

[SetUp]
public void SetUp()
{
aCalc = new Calc();
}

[TearDown]
public void TearDown()
{
aCalc = null;
}

[Test]
public void TestAdd()
{
int x;
int y;
int returnValue;
// TODO: Setup call parameters
returnValue = aCalc.Add(x, y);
// TODO: Validate return value
}

[Test]
public void TestSub()
{
int x;
int y;
int returnValue;
// TODO: Setup call parameters
returnValue = aCalc.Sub(x, y);
// TODO: Validate return value
}
}
}

Note: Each test method is automatically decorated with the [Test]
attribute in C# projects. In addition, in C# the test methods are defined as functions returning void. The following example shows a small Delphi for .NET program that performs simple addition and subtraction:

unit CalcUnit;

// .Net Version

interface

type

{ TCalc }

  TCalc = class
  public
    function Add(x, y: Integer): Integer;
    function Sub(x, y: Integer): Integer;
  end;

implementation

{ TCalc }

function TCalc.Add(x, y: Integer): Integer;
begin
  Result := x + y;
end;

function TCalc.Sub(X, Y: Integer): Integer;
begin
  Result := x + y;
end;

end.

The following example shows the test case skeleton file that you need to modify to test the two functions, Add and Sub, in the preceding code.

unit TestCalcUnit;

interface

uses
  NUnit.Framework, CalcUnit;

type
  // Test methods for class TCalc
  [TestFixture]
  TestTCalc = class
  strict private
    FCalc: TCalc;
  public
    [SetUp]
    procedure SetUp;
    [TearDown]
    procedure TearDown;
  published
    [Test]
    procedure TestAdd;
    [Test]
    procedure TestSub;
  end;

implementation

procedure TestTCalc.SetUp;
begin
  FCalc := TCalc.Create;
end;

procedure TestTCalc.TearDown;
begin
  FCalc := nil;
end;

procedure TestTCalc.TestAdd;
var
  ReturnValue: Integer;
  y: Integer;
  x: Integer;
begin
  // TODO: Setup call parameters
  ReturnValue := FCalc.Add(x, y);
  // TODO: Validate return value
end;

procedure TestTCalc.TestSub;
var
  ReturnValue: Integer;
  y: Integer;
  x: Integer;
begin
  // TODO: Setup call parameters
  ReturnValue := FCalc.Sub(x, y);
  // TODO: Validate return value
end;

end.

Note: In Delphi for .NET the test methods are defined as procedures.
Each test method must:
  • be public
  • be a procedure for Delphi for .NET or a function with a void return type for C#
  • take no arguments
 

Setup

Use the SetUp procedure to initialize variables or otherwise prepare your tests prior to running. For example, this is where you would set up a database connection, if needed by the test.

TearDown

The TearDown method can be used to clean up variable assignments, clear memory, or perform other maintenance tasks on your tests. For example, this is where you would close a database connection.

NUnit provides a number of asserts that you can use in your tests.

Function 
Description 
Syntax 
AreEqual  
Checks to see that two items are equal.  
Assert.AreEqual(expected, actual [, string message])  
IsNull  
Checks to see that an item is null.  
Assert.IsNull(object [, string message])  
IsNotNull  
Checks to see that an item is not null.  
Assert.IsNotNull(object [, string message])  
AreSame  
Checks to see that two items are the same.  
Assert.AreSame(expected, actual [, string message])  
IsTrue  
Checks to see that an item is True.  
Assert.IsTrue(bool condition [, string message])  
IsFalse  
Checks to see that an item is False.  
Assert.IsFalse(bool condition [, string message])  
Fail  
Fails the test.  
Assert.Fail([string message])  

You can use multiple asserts in any test method. This collection of asserts should test the common functionality of a given method. If an assert fails, the entire test method fails and any other assertions in the method are ignored. Once you fix the failing test and rerun your tests, the other assertions will be executed, unless one of them fails.

A test runner allows you to run your tests independently of your application. The NUnit test runners work by loading an assembly that contains the unit tests. The test runners identify the tests in the assembly from the [TEST] attributes generated by the Test Case Wizard.  

NUnit includes two test runner executables:

  • NUnitConsole.exe — This is the Console Test Runner, a text-based test runner that sends test output to the console.
  • NUnitGUI.exe — This is the GUI Test Runner, an interactive GUI-based test runner, with results color-coded for success or failure.
The GUI Test Runner is very useful when actively developing unit tests or the code you are testing. The GUI Test Runner displays a green indicator for a test that completes successfully, a red indicator for a test that fails, and a yellow indicator for a test that is skipped. 

The Console Test Runner is useful when you need to run completed code and tests from automated build scripts. If you want to redirect the output to a file, use the redirection command parameter. The following example shows how to redirect test results to a TestResult.txt text file:

 nunit-console nunit.tests.dll /out:TestResult.txt

Note: You may need to set the path to your host application in the Project Options
dialog. Set the Host Application property to the location of the test runner you want to use.

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!