相关疑难解决方法(0)

如何在TTestSetup类中访问TTestCase的字段

我正在用DUnit创建单元测试.我有一个类需要很长时间才能初始化.

我从TTestSetup派生出一个类TMyTestSetup并覆盖其Setup方法.对于我的TTestCase中的所有测试,只调用一次此SetUp方法.我将初始化过程放在TMyTestSetup.SetUp例程中以提高性能.

我的问题是如何访问我想要初始化的对象,这是TestSetup类中我的TMyTest的一个字段?唯一的方法是在全球范围内宣布它吗?

未经测试的简短例子:

TMyTestSetup = class(TTestSetup)
  protected
    procedure SetUp; override;
end;

TMyTest = class(TTestcase)
public
    fTakes4Ever2Init : TInits4Ever2Init;
published
  procedure Test1;     
end;

implementation

procedure TMyTestSetup.Setup;
begin
   // How can I access fTakes4Ever2Init from here?
  fTakes4Ever2Init.create // This is the call that takes long
end;

procedure TMyTest.Test1;
begin
  fTakes4Ever2Init.DoSomething;
end;

initialization
  RegisterTest(TMyTestSetup.Create(TMyTest.Suite));
Run Code Online (Sandbox Code Playgroud)

delphi unit-testing dunit

12
推荐指数
2
解决办法
1525
查看次数

标签 统计

delphi ×1

dunit ×1

unit-testing ×1