Currently I am using 2-level test hierarchy in DUnit (Test Project -> Test Case -> Test method; see example below). Is it possible to introduce 3rd level or even more levels?
3 Answers
I build a hierarchy by putting backslashes in the `SuitePath'. For instance:
initialization
RegisterTests('Group1\Group2', [TExampleTests1.Suite,
TExampleTests2.Suite]);
RegisterTests('Group1\Group3', [TExampleTests3.Suite,
TExampleTests4.Suite]);
end.
In the end I get something like this:
A lot less mucking around than with David's way, and you can spread your group definitions across disparate units.

- 5,414
- 26
- 38
You can use test suites to create as many levels of nesting as you desire. The documentation offers the following example:
The
TestFramework
unit exposes theTTestSuite
class, the class that implements test suites, so you can create test hierarchies using more explicit code:The following function,
UnitTests
, creates a test suite and adds the two test classes to it:function UnitTests: ITestSuite; var ATestSuite: TTestSuite; begin ATestSuite := TTestSuite.create('Some trivial tests'); ATestSuite.addTest(TTestArithmetic.Suite); ATestSuite.addTest(TTestStringlist.Suite); Result := ATestSuite; end;
Yet another way to implement the above function would be:
function UnitTests: ITestSuite; begin Result := TTestSuite.Create( 'Some trivial tests', [TTestArithmetic.Suite, TTestStringlist.Suite] ); end;
In the above example, the
TTestSuite
constructor adds the tests in the passed array to the suite.You can register a test suite created in any of the above ways by using the same call you use to register individual test cases:
initialization RegisterTest('Simple Test', UnitTests); end.
When run with
GUITestRunner
, you will see the new hierarchy.

- 601,492
- 42
- 1,072
- 1,490
-
6@Serg You can also add dots in the name under which you register the tests and DUnit will add a level per dot. For example, when you do `RegisterTest('Simple.Test', UnitTests);` It will create Simple as a node and Test as a subnode of that in the test hierarchy. Very nice feature to group tests in different units under the same functional name. – Marjan Venema Jan 16 '12 at 11:38
-
@Marjan +1 that's cool, didn't know that. I have a feeling that there's a lot of power in DUnit that is not widely know. – David Heffernan Jan 16 '12 at 12:05
-
1@DavidHeffernan yep, found this little gem by accident as well :-) – Marjan Venema Jan 16 '12 at 12:12
-
Only sad thing about DUnit is that its "trunk" now only works with Delphi 2007 and newer - so for older releases of Delphi a separate (older) version is required. – mjn Jan 16 '12 at 14:06
-
1@mjn: That was an unintentional break. The admins are busy so getting it fixed is taking ages, but the intention is that it work back through Delphi 7. – Zoë Peterson Jan 16 '12 at 16:00
You can group related tests in test suites, which can be nested.
If you want to do it at run time, check out my "Open Component Test Framework (OpenCTF)" at sourceforge.

- 36,362
- 28
- 176
- 378