- DelphiTools - https://www.delphitools.info -

Common Unit Tests for Delphi / FreePascal

Serg recent wrote an introduction to unit testing under Lazarus [1], showing how everything is there, but just that little bit “off” because of different unit names between FPCUnit and DUnit.

Not being a fan of ifdef, the prospect of having unit tests “uses” sections littered with ifdef did not attract me, so I made a little adapter unit [2] to keep the “uses” sections clean.

It simply aliases the useful unit tests classes and units and goes like:

unit dwsXPlatformTests;

interface

uses
   Classes, SysUtils,
   {$ifdef FPC}
   fpcunit, testutils, testregistry
   {$else}
   TestFrameWork
   {$endif}
   ;

type

   {$ifdef FPC}
   TTestCase = fpcunit.TTestCase;
   {$else}
   TTestCase = TestFrameWork.TTestCase;
   {$endif}

procedure RegisterTest(const testName : String; aTest : TTestCaseClass);

implementation

// RegisterTest
//
procedure RegisterTest(const testName : String; aTest : TTestCaseClass);
begin
   {$ifdef FPC}
   testregistry.RegisterTest(aTest);
   {$else}
   TestFrameWork.RegisterTest(testName, aTest.Suite);
   {$endif}
end;

With it, unit test cases can just refer “dwsXPlatformTests”, which encapsulates the ugly ifdef’ing.