Unit testing in Pascal
last modified November 19, 2026 Free Pascal 3.2.2
A test is a small program that calls the code and compares the result with the
expected value. Pascal has no test framework in the language, but a usable
harness is a counter and an Assert, and the FPCUnit framework in
the FCL provides test cases, suites, and a console runner. This article builds
both: a minimal harness that fits in one file, and an FPCUnit suite that
reports failures and sets the process exit status.
A minimal harness
The core of every test runner is the same: run a check, count a pass or a
failure, and exit with a non-zero status if anything failed. The
Check procedure below takes a Boolean and a name, prints one line,
and updates two counters. The function under test is deliberately simple so
that the output is easy to read.
program harness;
{$mode objfpc}{$H+}
uses
SysUtils;
var
Passed, Failed: Integer;
procedure Check(Condition: Boolean; const Name: string);
begin
if Condition then
begin
Inc(Passed);
WriteLn('ok ', Name);
end
else
begin
Inc(Failed);
WriteLn('FAIL ', Name);
end;
end;
function Add(A, B: Integer): Integer;
begin
Result := A + B;
end;
begin
Passed := 0;
Failed := 0;
Check(Add(2, 2) = 4, 'adds two and two');
Check(Add(-1, 1) = 0, 'adds opposites');
Check(Add(2, 2) = 5, 'detects a wrong sum');
WriteLn(Format('%d passed, %d failed', [Passed, Failed]));
if Failed > 0 then
Halt(1);
end.
The third check is intentionally wrong, so the run has one failure and the
process exits with status 1. A build server or a shell script can use that
status without parsing the text. Halt(1) flushes the output and
terminates immediately; after the summary there is nothing left to do.
$ fpc -gl harness.pas $ ./harness ok adds two and two ok adds opposites FAIL detects a wrong sum 2 passed, 1 failed
FPCUnit test cases
FPCUnit is the xUnit framework shipped with Free Pascal. A test case is a
class derived from TTestCase; every published parameterless
method whose name starts with Test is a test. Assertions such as
AssertEquals, AssertTrue, and AssertNotNull
raise EAssertionFailedError when they fail, and the runner records
the failure instead of stopping the suite.
program runner;
{$mode objfpc}{$H+}
uses
cthreads, Classes, SysUtils, fpcunit, testregistry, consoletestrunner;
type
TCalculatorTest = class(TTestCase)
published
procedure TestAdd;
procedure TestAddNegative;
end;
procedure TCalculatorTest.TestAdd;
begin
AssertEquals(4, 2 + 2);
end;
procedure TCalculatorTest.TestAddNegative;
begin
AssertEquals(0, -1 + 1);
end;
var
App: TTestRunner;
begin
RegisterTest(TCalculatorTest);
App := TTestRunner.Create(nil);
App.Initialize;
App.Run;
App.Free;
end.
RegisterTest adds the class to the global registry, and
TTestRunner discovers the published methods through RTTI. The
cthreads unit must come first on Unix. Initialize
parses the command line, and Run executes the suite; without
--all the runner prints its usage instead of running anything.
Running the suite
The plain text format with timings disabled is the easiest to read and to
compare between runs. --all selects every registered test, and
--format=plainnotiming removes the clock values that would
otherwise change on every execution.
The first line counts the whole run: two tests, zero errors, zero failures, and zero ignored tests. The nested line repeats the counts for the test case class, and the indented names are the individual methods. The process exits with status 0 when no test fails.
$ fpc -gl runner.pas
$ ./runner --all --format=plainnotiming
N:2 E:0 F:0 I:0
TCalculatorTest N:2 E:0 F:0 I:0
TestAdd
TestAddNegative
Number of run tests: 2
Number of errors: 0
Number of failures: 0
A failing test
A failing assertion reports the expected and the actual value, the test method,
and the exception class. The next version adds a third method that expects
2 + 2 to be 5; the runner continues with the remaining tests and
finishes with a non-zero status.
program runnerfail;
{$mode objfpc}{$H+}
uses
cthreads, Classes, SysUtils, fpcunit, testregistry, consoletestrunner;
type
TCalculatorTest = class(TTestCase)
published
procedure TestAdd;
procedure TestAddNegative;
procedure TestWrong;
end;
procedure TCalculatorTest.TestAdd;
begin
AssertEquals(4, 2 + 2);
end;
procedure TCalculatorTest.TestWrong;
begin
AssertEquals(5, 2 + 2);
end;
procedure TCalculatorTest.TestAddNegative;
begin
AssertEquals(0, -1 + 1);
end;
var
App: TTestRunner;
begin
RegisterTest(TCalculatorTest);
App := TTestRunner.Create(nil);
App.Initialize;
App.Run;
App.Free;
end.
The at line gives the address of the failing assertion, which
depends on the build; the message and the exception class are the portable
parts. --sparse hides the passing tests and prints only the
failures, which is convenient on a large suite.
$ fpc -gl runnerfail.pas
$ ./runnerfail --all --format=plainnotiming --sparse
N:3 E:0 F:1 I:0
TCalculatorTest N:3 E:0 F:1 I:0
TestWrong Failed: expected: <5> but was: <4>
Exception: expected: <5> but was: <4>
at $00000000004847A0
Number of run tests: 3
Number of errors: 0
Number of failures: 1
List of failures:
Failure:
Message: TCalculatorTest.TestWrong: expected: <5> but was: <4>
Exception class: EAssertionFailedError
at $00000000004847A0
Setup, teardown, and floating point
Override SetUp to create the objects that every test needs and
TearDown to free them; the runner calls both around each test
method. This keeps each test independent and makes a failure repeatable.
Fixtures that are expensive to build can be created once in the suite, but
per-test setup is the safer default.
Comparing real numbers with = is unreliable. FPCUnit has an
overload of AssertEquals that takes a tolerance:
AssertEquals(0.3, 0.1 + 0.2, 1e-9). The third argument is the
largest acceptable difference, not a percentage. For strings, compare exact
values; for output that contains dates or addresses, compare a stable part or
normalize the text before the comparison.
Source
Free Pascal: testregistry unit
In this article we wrote a minimal test harness, defined FPCUnit test cases, ran a passing and a failing suite, and used a tolerance for real numbers. The compiling and debugging article explains the runtime checks that complement these tests, and the style guide covers the naming conventions that make a suite readable.
Author
List all Pascal tutorials.