目前我在DUnit中使用二级测试层次结构(测试项目-> 测试用例->测试方法;请参见下面的示例)。是否可以引入第三个或更多级别?
我通过在`SuitePath'中加入反斜杠来构建层次结构。例如:
initialization
RegisterTests('Group1\Group2', [TExampleTests1.Suite,
TExampleTests2.Suite]);
RegisterTests('Group1\Group3', [TExampleTests3.Suite,
TExampleTests4.Suite]);
end.
最终我得到了像这样的东西:
相比David的方式,这要少得多,而且你可以将组定义分散在不同的单元中。您可以使用测试套件创建任意层次的嵌套。文档提供了以下示例:(链接)
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.
您可以将相关测试分组到测试套件中,这些套件可以嵌套。
如果您想在运行时进行操作,请查看我的“Open Component Test Framework (OpenCTF)”:sourceforge。
RegisterTest('Simple.Test', UnitTests);
时,它将在测试层次结构中创建 Simple 节点和 Test 作为该节点的子节点。这是一项非常不错的功能,可将不同单元中的测试分组在同一个功能名称下。 - Marjan Venema