How to access fields of a TTestCase in a TTestSetup class

后端 未结 7 1269
死守一世寂寞
死守一世寂寞 2021-02-07 13:37

I am creating unit tests with DUnit. I have a class that takes quite a long time to initialize.

I derive a class TMyTestSetup from TTestSetup and override its Setup met

7条回答
  •  猫巷女王i
    2021-02-07 14:29

    The trick is to use a public class variable in the TMyTestSetup class.

    Like this (tested and working, complete) example:

    unit TestTestUnit;
    
    interface
    
    uses
      TestFramework, TestExtensions;
    
    type
      TInits4Ever2Init = class
      private
        FValue: integer;
      public
        constructor Create;
        procedure   DoSomething1;
        procedure   DoSomething2;
        procedure   DoSomething3;
      end;
    
    type
      TMyTestSetup = class(TTestSetup)
      public class var
        fTakes4Ever2Init: TInits4Ever2Init;
      protected
        procedure SetUp; override;
      end;
    
      TMyTest = class(TTestCase)
      published
        procedure Test1;
        procedure Test2;
        procedure Test3;
      end;
    
    implementation
    
    uses
      SysUtils, Windows;
    
    { TMyTestSetup }
    
    procedure TMyTestSetup.Setup;
    begin
      fTakes4Ever2Init := TInits4Ever2Init.create; // This is the call that takes long
    end;
    
    { TMyTest }
    
    procedure TMyTest.Test1;
    begin
      TMyTestSetup.fTakes4Ever2Init.DoSomething1;
    end;
    
    procedure TMyTest.Test2;
    begin
      TMyTestSetup.fTakes4Ever2Init.DoSomething2;
    end;
    
    procedure TMyTest.Test3;
    begin
      TMyTestSetup.fTakes4Ever2Init.DoSomething3;
    end;
    
    { TInits4Ever2Init }
    
    constructor TInits4Ever2Init.Create;
    begin
      inherited Create;
    
      // FValue and Format('%p, %d', [Pointer(Self), FValue])) are to confirm
      //   that we are talking to the same object for all the tests,
      //   but that the object is different each time we run the test suite.
    
      Randomize;
      FValue := Random(10000);
    
      OutputDebugString(pAnsiChar('-- TInits4Ever2Init.Create: '
        + Format('%p, %d', [Pointer(Self), FValue])));
    end;
    
    procedure TInits4Ever2Init.DoSomething1;
    begin
      OutputDebugString(pAnsiChar('-- TInits4Ever2Init.DoSomething1: '
        + Format('%p, %d', [Pointer(Self), FValue])));
    end;
    
    procedure TInits4Ever2Init.DoSomething2;
    begin
      OutputDebugString(pAnsiChar('-- TInits4Ever2Init.DoSomething2: '
        + Format('%p, %d', [Pointer(Self), FValue])));
    end;
    
    procedure TInits4Ever2Init.DoSomething3;
    begin
      OutputDebugString(pAnsiChar('-- TInits4Ever2Init.DoSomething3: '
        + Format('%p, %d', [Pointer(Self), FValue])));
    end;
    
    initialization
      RegisterTest(TMyTestSetup.Create(TMyTest.Suite));
    end.
    

    As the comments in the sample indicate, I have used a randomised private variable, and some debug trace output, to confirm that each test call with the test suite is to the same copy of the target object, but that we are getting a different copy of the target object each time the test suite is run.

提交回复
热议问题