12.3 release available - Learn More!

DUnitX Overview

From RAD Studio
Jump to:navigation,search

Go Up toTesting Applications Index

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.

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, macOS, and Linux in Delphi 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 uses Generics and Anonymous methods.DUnitX uses attributes under Delphi personality.

Also see the DUnitX home page athttps://github.com/VSoftTechnologies/DUnitX/wiki.

Developing Delphi DUnitX Tests

The following sample Delphi program defines two functions that perform a 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 unit that you need to modify to test the two functions. You have to add code to the and methods as well as to the test methods and. Finally, make use of attributes to test the functions.

unitTestCalcUnit;interfaceusesDUnitX.TestFramework,CalcUnit;type[TestFixture]TestTCalc=class(TObject)strictprivateaTCalc:TCalc;public[Setup]procedureSetup;[TearDown]procedureTearDown;// Sample Methods// Test with TestCase Atribute to supply parameters.[TestCase('TestA','8,2,10')]procedureTestAdd(Value1,Value2,_Result:Integer);// Test with TestCase Atribute to supply parameters.[TestCase('TestB','3,4,-1')]procedureTestSub(Value1,Value2,_Result:Integer);end;implementationprocedureTestTCalc.Setup;beginaTCalc:=TCalc.Create;end;procedureTestTCalc.TearDown;beginaTCalc:=nil;end;procedureTestTCalc.TestAdd(Value1,Value2,_Result:Integer);varR:Integer;beginR:=aTCalc.Add(Value1,Value2);Assert.AreEqual(R,_Result);// testcodeend;procedureTestTCalc.TestSub(Value1,Value2,_Result:Integer);varR:Integer;beginR:=aTCalc.Sub(Value1,Value2);Assert.AreEqual(R,_Result);// testcodeend;initializationTDUnitX.RegisterTestFixture(TestTCalc);end.

DUnitX Functions

DUnitX providesAssert class with a number of functions that you can use in your tests.

The table shows some of these functions.

FunctionDescription

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.

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 providesTDUnitXConsoleLogger class to run the testing output to the console.
    • Is useful when you want to run completed code and tests from automated build scripts.
  • The GUI Test Runner:
    • Displays test results interactively in a GUI window. DUnitX framework provides aTGUIXTestRunner 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.

See Also

Retrieved from "https://docwiki.embarcadero.com/RADStudio/Sydney/e/index.php?title=DUnitX_Overview&oldid=270553"
Categories:
Hidden category: