12.3 release available - Learn More!

DUnit Overview

From RAD Studio
Jump to:navigation,search

Go Up toTesting Applications Index

Attention: Bear in mind thatDUnit does not support the FireMonkey GUI to view your output; useDUnitX instead.
Note: DUnitX uses Delphi language features that are not supported in C++. For C++ developers, DUnit is the best tool. C++ can also be tested using other frameworks such as Google Test, available viaGetIt Package Manager.

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.

Also, see the DUnit home page athttp://dunit.sourceforge.net/

Contents

Installing DUnit

To install the DUnit feature follow these steps:

  1. Select to open theFeature Manager.
  2. On theAdditional Options tab, check the DUnit Testing Frameworks feature to add it to your RAD Studio installation.
  3. SelectApply to apply the selected changes.

When the changes are applied, theFeature Manager shows anOperation Complete screen.

Developing Delphi DUnit Tests

Every DUnit test implements a class of type.

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

unitCalcUnit;interfacetype{ TCalc }TCalc=classpublicfunctionAdd(x,y:Integer):Integer;functionSub(x,y:Integer):Integer;end;implementation{ TCalc }functionTCalc.Add(x,y:Integer):Integer;beginResult:=x+y;end;functionTCalc.Sub(X,Y:Integer):Integer;beginResult:=x-y;end;end.

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

unitTestCalcUnit;interfaceusesTestFramework,CalcUnit;type// Test methods for class TCalcTestTCalc=class(TTestCase)strictprivateaTCalc:TCalc;publicprocedureSetUp;override;procedureTearDown;override;publishedprocedureTestAdd;procedureTestSub;end;implementationprocedureTestTCalc.SetUp;beginaTCalc:=TCalc.Create;end;procedureTestTCalc.TearDown;beginaTCalc:=nil;end;procedureTestTCalc.TestAdd;var_result:System.Integer;y:System.Integer;x:System.Integer;begin_result:=aTCalc.Add(x,y);// TODO: Add testcode hereend;procedureTestTCalc.TestSub;var_result:System.Integer;y:System.Integer;x:System.Integer;begin_result:=aTCalc.Sub(x,y);// TODO: Add testcode hereend;initialisation// Register any test cases with the test runnerRegisterTest(TestTCalc.Suite);end.

Developing C++ DUnit Tests

Every DUnit test implements a class of type.

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

 #ifndef Unit7H #define Unit7H//-------------------------------------------------classTCalc{public:intAdd(intx,inty);intSub(intx,inty);}; #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 and. The example illustrates the DUnit scaffolding for Unit Tests.

 #include <vcl.h> #pragma hdrstop #include <TestFramework.hpp>classTTestTCalc:publicTTestCase{public:__fastcallvirtualTTestTCalc(AnsiStringname):TTestCase(name){}virtualvoid__fastcallSetUp();virtualvoid__fastcallTearDown();__published:void__fastcallTestAdd();void__fastcallTestSub();};void__fastcallTTestTCalc::SetUp(){}void__fastcallTTestTCalc::TearDown(){}void__fastcallTTestTCalc::TestAdd(){// int Add(int x, int y)}void__fastcallTTestTCalc::TestSub(){// int Sub(int x, int y)}staticvoidregisterTests(){_di_ITestSuiteiSuite;TTestSuite*testSuite=newTTestSuite("Testing Unit7.h");if(testSuite->GetInterface(iSuite)){testSuite->AddTests(__classid(TTestTCalc));Testframework::RegisterTest(iSuite);}else{deletetestSuite;}} #pragma startup registerTests 33

DUnit Functions

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

FunctionDescription

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.

DUnit Test Runners

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.

See Also

Retrieved from "https://docwiki.embarcadero.com/RADStudio/Sydney/e/index.php?title=DUnit_Overview&oldid=272432"
Categories: