Unit tests for Free Pascal and Delphi

Is there a way to write unit tests so that they can be compiled and run with both Delphi and Free Pascal?

There are different unit test frameworks for Delphi and Free Pascal, which leads to duplication of work for developers who target both compilers (for example, developers of libraries and frameworks).

So there may be a way using the DUnit or FPCUnit structure and customizing the source code of the test case (or the frame itself) so that it also works with another compiler.

So essentially the question is:

  • which structure (DUnit or FPCUnit) can be compiled with both compilers (Delphi and Free Pascal) with minimal changes?

or

  • Is there a third structure (thanks Arno for mentioning TSynTest) that works with Delphi and FPC?
+8
unit-testing delphi freepascal dunit
source share
3 answers

By default, the unit test framework for Free Pascal is FPCUnit, it has the same design as DUnit, but differs from it in small details. You can write general unit tests for FPCUnit and DUnit, bypassing the differences with {$IFDEF FPC} . I just tested FPCUnit, it is a convenient platform, and wrote about it on the blog .

+6
source share

See this very nice blog article - only fresh meat about testing FPCUnit.

In short, as far as I know, and if you compare with DUnit :

  • Most of the validation methods * () have been renamed Assert * ();
  • SetUp / TearDown methods are called per-function in both frames;
  • Some other thoughts may vary.

So, I think it would be easy to let FPCUnit "mimic" DUnit by creating a small wrapper class over the implementation of FPCUnit to have the exact same methods as with DUnit. That way, you can use code between two targets and even reuse existing DUnit tests. This IMHO wrapper class is much more convenient if you use {$ifdef FPC} like the others offered here. Conditional compilation usually makes code difficult to debug, verbose, redundant, and should only be used when necessary.

Another potential solution might be to use a different test framework. Our little TSynTest classes are lighter, but I am currently converting the framework to FPC. Thus, the same exact code can be used with both compilers. It has some features (such as optional fine-grained logging and a full stack on failure) that I would have missed from DUnit / FPCUnit. It does not have a graphical interface or a wizard, but to be honest, as a programmer, I prefer simple text that can easily be included in my technical documentation to attest that the regression did not happen.

+10
source share

I just hacked a sample that works both in DUnit (delphi) and in FPCUnit (the Freepascal equivalent closest to DUnit, which is already sent "to the field" in lazarus 1.0, which includes freepascal 2.6):

The trivial bit of IFDEF, and you're there.

 unit TestUnit1; {$IFDEF FPC} {$mode objfpc}{$H+} {$ENDIF} interface uses Classes, {$ifdef FPC} fpcunit, testutils, testregistry, {$else} TestFramework, {$endif} SysUtils; type TTestCase1= class(TTestCase) published procedure TestHookUp; end; implementation procedure TTestCase1.TestHookUp; begin Self.Check(false,'value'); end; initialization RegisterTest(TTestCase1{$ifndef FPC}.Suite{$endif}); end. 
+3
source share