Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single-source unit tests for Free Pascal and Delphi

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

There are different unit test frameworks for Delphi and Free Pascal, which causes duplicate work for developers who target both compilers (for example, library and framework developers).

So maybe there is a way, using either the DUnit or the FPCUnit framework and tweak the test case source code (or the framework itself) so that it also works with the other compiler.

So essentially the question is:

  • which framework (DUnit or FPCUnit) can be compiled with both compilers (Delphi and Free Pascal) with as little modifications as possible?

or

  • is there a third framework (Thanks to Arnaud for mentioning TSynTest) which works with Delphi and FPC?
like image 700
mjn Avatar asked Aug 30 '12 19:08

mjn


People also ask

What is single unit testing?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.

What is the best unit test framework for C ++?

The most scalable way to write unit tests in C is using a unit testing framework, such as: CppUTest. Unity. Google Test.

Does C++ have unit testing?

You can write and run your C++ unit tests by using the Test Explorer window. It works just like it does for other languages. For more information about using Test Explorer, see Run unit tests with Test Explorer.


2 Answers

I just whipped up a sample that works in both DUnit (delphi) and FPCUnit (Freepascal equivalent nearest to DUnit, that happens to ship already "in the box" in lazarus 1.0, which includes freepascal 2.6 ):

A 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.
like image 71
Warren P Avatar answered Sep 29 '22 05:09

Warren P


Default unit test framework for Free Pascal is FPCUnit, it has the same design as DUnit but different from it in minor details. You can write common unit tests for FPCUnit and DUnit by circumventing the differences by {$IFDEF FPC}. I just tested FPCUnit, it is a usable framework, and blogged about it.

like image 44
kludg Avatar answered Sep 29 '22 07:09

kludg