DUnit tests hierarchy

1.1k views Asked by At

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?

DUnit Example

3

There are 3 answers

5
David Heffernan On BEST ANSWER

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 the TTestSuite 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.

0
mjn On

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.

0
Nat On

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:

Example DUnit Test Hierarchy

A lot less mucking around than with David's way, and you can spread your group definitions across disparate units.