DUnit测试层次结构

13

目前我在DUnit中使用二级测试层次结构(测试项目-> 测试用例->测试方法;请参见下面的示例)。是否可以引入第三个或更多级别?

DUnit示例

3个回答

11

我通过在`SuitePath'中加入反斜杠来构建层次结构。例如:

initialization

  RegisterTests('Group1\Group2', [TExampleTests1.Suite,
                                  TExampleTests2.Suite]);

  RegisterTests('Group1\Group3', [TExampleTests3.Suite,
                                  TExampleTests4.Suite]);
end.
最终我得到了像这样的东西:

示例DUnit测试层次结构

相比David的方式,这要少得多,而且你可以将组定义分散在不同的单元中。

9

您可以使用测试套件创建任意层次的嵌套。文档提供了以下示例:(链接)

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.


6
@Serg,您还可以在注册测试时的名称中添加点号,DUnit 将根据每个点号添加一个级别。例如,当您执行 RegisterTest('Simple.Test', UnitTests); 时,它将在测试层次结构中创建 Simple 节点和 Test 作为该节点的子节点。这是一项非常不错的功能,可将不同单元中的测试分组在同一个功能名称下。 - Marjan Venema
@Marjan +1,太酷了,我不知道这个。我有一种感觉,DUnit 中有很多强大的功能并不为人所知。 - David Heffernan
1
@DavidHeffernan 是的,我也是偶然发现了这个小宝石 :-) - Marjan Venema
DUnit 的唯一遗憾是,它的“主干”现在只适用于 Delphi 2007 及更高版本 - 因此,对于旧版本的 Delphi,需要使用单独的(较旧的)版本。 - mjn
1
@mjn:那是一个无意的中断。管理员们很忙,所以修复它需要很长时间,但意图是让它能够通过Delphi 7再次工作。 - Zoë Peterson

3

您可以将相关测试分组到测试套件中,这些套件可以嵌套。

如果您想在运行时进行操作,请查看我的“Open Component Test Framework (OpenCTF)”:sourceforge


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接