DUnitX Overview
Go Up to Testing Applications Index
DUnitX is an open-source unit test framework based on the NUnit test framework, including some ideas from xUnit as well. The RAD Studio integration of DUnitX framework enables you to develop and execute tests against Win32, Win 64 and MacOSX in Delphi and C++Builder applications.
The DUnitX testing framework provides its own set of methods for testing conditions. You can use the provided methods to test a large number of conditions. These methods represent common assertions although you can also create your own custom assertions.
DUnitX makes use of Generics and Anonymous methods. DUnitX uses attributes under Delphi personality. In C++Builder, DUnitX uses published methods.
Also see the DUnitX home page at https://github.com/VSoftTechnologies/DUnitX/wiki.
Contents
Developing Delphi DUnitX Tests
The following sample Delphi program defines two functions that perform a 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 unit that you need to modify to test the two functions. You have to add code to the SetUp and TearDown methods as well as to the test methods TestAdd and TestSub. Finally, make use of attributes to test the functions.
unit TestCalcUnit;
interface
uses
DUnitX.TestFramework, CalcUnit;
type
[TestFixture]
TestTCalc = class(TObject)
strict private
aTCalc: TCalc;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
// Sample Methods
// Test with TestCase Atribute to supply parameters.
[TestCase('TestA','8,2,10')]
procedure TestAdd(Value1, Value2, _Result: Integer);
// Test with TestCase Atribute to supply parameters.
[TestCase('TestB','3,4,-1')]
procedure TestSub(Value1, Value2, _Result: Integer);
end;
implementation
procedure TestTCalc.Setup;
begin
aTCalc := TCalc.Create;
end;
procedure TestTCalc.TearDown;
begin
aTCalc := nil;
end;
procedure TestTCalc.TestAdd(Value1, Value2, _Result: Integer);
var
R: Integer;
begin
R := aTCalc.Add(Value1, Value2);
Assert.AreEqual(R, _Result); // testcode
end;
procedure TestTCalc.TestSub(Value1, Value2, _Result: Integer);
var
R: Integer;
begin
R := aTCalc.Sub(Value1, Value2);
Assert.AreEqual(R, _Result); // testcode
end;
initialization
TDUnitX.RegisterTestFixture(TestTCalc);
end.
Developing C++ DUnitX Tests
The following sample C++Builder program defines two functions that perform a simple addition and subtraction:
class TCalc
{
public:
int Add(int x,int y);
int Sub(int x,int y);
};
int TCalc::Add(int x, int y)
{
int Result = x + y;
return Result;
}
int TCalc::Sub(int x, int y)
{
int Result = x-y;
return Result;
}
The following example shows the test unit that you need to modify to test the two functions. You have to add code to the SetUp and TearDown methods as well as to the test methods TestAdd and TestSub.
#include <DUnitX.TestFramework.hpp>
#include <stdio.h>
#pragma option --xrtti
class __declspec(delphirtti) TestCalc : public TObject
{
public:
virtual void __fastcall SetUp();
virtual void __fastcall TearDown();
__published:
void __fastcall TestAdd();
void __fastcall TestSub();
};
TCalc *aTCalc;
void __fastcall TestCalc::SetUp()
{
aTCalc = new TCalc();
}
void __fastcall TestCalc::TearDown()
{
delete aTCalc;
}
void __fastcall TestCalc::TestAdd()
{
int R;
R = aTCalc->Add(2, 8);
Assert::AreEqual(R, 10);
}
void __fastcall TestCalc::TestSub()
{
int R;
R = aTCalc->Add(3, 4);
Assert::AreEqual(R, -1);
}
static void registerTests()
{
TDUnitX::RegisterTestFixture(__classid(TestCalc));
}
#pragma startup registerTests 33
DUnitX Functions
DUnitX provides Assert class with a number of functions that you can use in your tests.
The table shows some of these functions.
Function | Description |
---|---|
Pass |
Checks that a routine works. |
Fail |
Checks that a routine fails. |
AreEqual |
Checks to see if items are equal. |
AreNotEqual |
Checks to see if items are not equal. |
AreSame |
Checks to see that two items have the same value. |
AreNotSame |
Checks to see that two items do not have the same value. |
Contains |
Checks to see if the item is in a list. |
DoesNotContain |
Checks to see if the item is not in a list. |
IsTrue |
Checks that a condition is true. |
IsFalse |
Checks that a condition is false. |
IsEmpty |
Checks to see if the value of an item is empty. |
IsNotEmpty |
Checks to see if the value of an item is not empty. |
IsNull |
Checks to see that an item is null. |
IsNotNull |
Checks to see that an item is not null. |
WillRaise |
Checks to see if the method will raise an exception. |
StartsWith |
Checks if a string starts with a specified substring. |
InheritsFrom |
Checks if a class is descendant of a specified class. |
IsMatch |
Checks if the item matches with a specified pattern. |
For more information about the syntax and usage of these and other DUnitX functions, see the DUnitX help files in \source\DUnitX.
DUnitX Test Runners
A test runner allows you to run your tests independently of your application. In a DUnitX test project, the test runner code from the DUnitX framework is compiled directly into the generated executable making the test project itself a test runner. The integrated DUnitX framework provides two test runners:
- The Console Test Runner:
- Sends all test output to the console. This is the testing mode by default when you run your DUnitX project. DUnitX framework provides
TDUnitXConsoleLogger
class to run the testing output to the console. - Is useful when you want to run completed code and tests from automated build scripts.
- Sends all test output to the console. This is the testing mode by default when you run your DUnitX project. DUnitX framework provides
- The GUI Test Runner:
- Displays test results interactively in a GUI window. DUnitX framework provides a
TGUIXTestRunner
class to generate the FireMonkey GUI output and aTDUnitXGuiLoggerForm
class for the VCL GUI. - Is very useful when actively developing unit tests for the code you are testing.
- Displays test results interactively in a GUI window. DUnitX framework provides a