RAD Studio (Common)
ContentsIndex
PreviousUpNext
DUnit Overview

DUnit is an open-source unit test framework based on the JUnit test framework. The DUnit framework enables you to build and execute tests against Delphi Win32 applications. The RAD Studio integration of DUnit framework enables you to develop and execute tests against Delphi Win32 and C++Builder applications.  

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

Every DUnit test implements a class of type TTestCase.  

The following sample Delphi Win32 program defines two functions that perform simple addition and subtraction:

unit CalcUnit;

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
  TestFramework, CalcUnit;
type
  // Test methods for class TCalc
  TestTCalc = class(TTestCase)
  strict private
    aTCalc: TCalc;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestAdd;
    procedure TestSub;
  end;

implementation

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

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

procedure TestTCalc.TestAdd;
var
  _result: System.Integer;
  y: System.Integer;
  x: System.Integer;
begin
  _result := aTCalc.Add(x, y);
  // TODO: Add testcode here
end;

procedure TestTCalc.TestSub;
var
  _result: System.Integer;
  y: System.Integer;
  x: System.Integer;
begin
  _result := aTCalc.Sub(x, y);
  // TODO: Add testcode here
end;

initialisation
  // Register any test cases with the test runner
  RegisterTest(TestTCalc.Suite);
end.

Every DUnit test implements a class of type TTestCase.  

The following sample C++ Win32 header file and program define two functions that perform simple addition and subtraction:

#ifndef Unit7H
#define Unit7H
//-------------------------------------------------


class TCalc
{
public:
  int Add(int x, int y);
  int Sub(int x, int y);
};


#endif

The following example (TestUnit7.cpp) contains a Testcase for the TCalc class. The Wizard generated this example, but the user is expected to write tests that exercise the functions Add and Sub. The example illustrates the DUnit scaffolding for Unit Tests.

#include <vcl.h>
#pragma hdrstop

#include <TestFramework.hpp>

class TTestTCalc : public TTestCase
{
public:
  __fastcall virtual TTestTCalc(AnsiString name) : TTestCase(name) {}
  virtual void __fastcall SetUp();
  virtual void __fastcall TearDown();

__published:
  void __fastcall TestAdd();
  void __fastcall TestSub();
};


void __fastcall TTestTCalc::SetUp()
{
}

void __fastcall TTestTCalc::TearDown()
{
}

void __fastcall TTestTCalc::TestAdd()
{
  // int Add(int x, int y)
}

void __fastcall TTestTCalc::TestSub()
{
  // int Sub(int x, int y)
}



static void registerTests()
{
  _di_ITestSuite iSuite;
  TTestSuite* testSuite = new TTestSuite("Testing Unit7.h");
  if (testSuite->GetInterface(iSuite)) {
    testSuite->AddTests(__classid(TTestTCalc));
    Testframework::RegisterTest(iSuite);
  } else {
    delete testSuite;
  }
}
#pragma startup registerTests 33


/* [For debug purposes only - To be removed soon!!]
GenerateHeaderComment=true
DefaultExtension=.cpp
FileName=C:\Users\bbabet\Documents\RAD Studio\Projects\DUnitSample\Test\TestUnit7.cpp
TestFramework=DUnit / C++ Win32
OutputPersonality=CPlusPlusBuilder.Personality
TestProject=C:\Users\bbabet\Documents\RAD Studio\Projects\DUnitSample\Test\Project3Tests.cbproj
UnitUnderTest=C:\Users\bbabet\Documents\RAD Studio\Projects\DUnitSample\Unit7.h
NameOfUnitUnderTest=Unit7.h
TestCaseBaseClass=TTestCase
TestCasePrefix=Test
UnitName=TestUnit7
Namespace=TestUnit7
TestClasses=
  <0>
    Name=TCalc
    Methods=
      <0>
        Name=Add
        Signature=int Add(int x, int y)
      <1>
        Name=Sub
        Signature=int Sub(int x, int y)
TestClass=
Method=
*/

DUnit provides a number of functions that you can use in your tests.

Function 
Description 
Check  
Checks to see if a condition was met.  
CheckEquals  
Checks to see that two items are equal.  
CheckNotEquals  
Checks to see if items are not equal.  
CheckNotNull  
Checks to see that an item is not null.  
CheckNull  
Checks to see that an item is null.  
CheckSame  
Checks to see that two items have the same value.  
EqualsErrorMessage  
Checks to see that an error message emitted by the application matches a specified error message.  
Fail  
Checks that a routine fails.  
FailEquals  
Checks to see that a failure equals a specified failure condition.  
FailNotEquals  
Checks to see that a failure condition does not equal a specified failure condition.  
FailNotSame  
Checks to see that two failure conditions are not the same.  
NotEqualsErrorMessage  
Checks to see that two error messages are not the same.  
NotSameErrorMessage  
Checks that one error message does not match a specified error message.  

For more information on the syntax and usage of these and other DUnit functions, see the DUnit help files in \source\dunit\doc.

A test runner allows you to run your tests independently of your application. In a DUnit test project, the test runner code from the DUnit framework is compiled directly into the generated executable making the test project itself a test runner. The integrated DUnit framework provides two test runners:

  • The GUI Test Runner — This displays test results interactively in a GUI window, with results color-coded to indicate success or failure.
  • The Console Test Runner — This sends all test output to the console.
The DUnit GUI Test Runner is very useful when actively developing unit tests for the code you are testing. The DUnit GUI Test Runner displays a green bar over a test that completes successfully, a red bar over a test that fails, and a yellow bar over a test that is skipped. 

The DUnit Console Test Runner is useful when you want to run completed code and tests from automated build scripts.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!