Ada unit testing
last modified November 23, 2026
A test is a program that checks another program and reports what went wrong.
Ada already contains the two pieces such a program needs: pragma
Assert turns a condition into a checked claim, and exceptions report a
broken claim with a message. A small package on top of them records how many
checks passed and failed, and the exit status tells a build server whether the
suite succeeded. This article starts with single assertions, builds a
reusable harness, tests a package with explicit calls and with a table of
cases, examines a failure, and runs the suite from a project file.
Assertions as the smallest test
pragma Assert takes a Boolean expression and an optional message.
When the expression is false, it raises Assertion_Error with that
message; when it is true, execution simply continues. The pragma is checked
only when the program is compiled with -gnata; without the
switch the compiler ignores it, as the contracts
article explains. The first program checks two sums of small integers.
with Ada.Text_IO; use Ada.Text_IO;
procedure Assert_Demo is
function Add (A, B : Integer) return Integer is (A + B);
begin
pragma Assert (Add (2, 3) = 5, "2 + 3 must be 5");
pragma Assert (Add (-4, 1) = -3, "-4 + 1 must be -3");
Put_Line ("all checks passed");
end Assert_Demo;
Both conditions hold, so the two pragmas do nothing and the final line is printed. The assertions are not comments: the compiler generates a check for each one, and the message is stored in the executable. A failing check stops the program at the point of the pragma, before any later statement runs.
$ gnatmake -gnat2022 -gnatwa -gnata assert_demo.adb $ ./assert_demo all checks passed
When an assertion fails
The next program contains one wrong claim. Add (2, 2) is four,
not five, so the assertion fails. There is no handler for
Assertion_Error, so the exception propagates out of the main
procedure, the runtime prints its name and message, and the process exits with
status 1.
with Ada.Text_IO; use Ada.Text_IO;
procedure Assert_Fail is
function Add (A, B : Integer) return Integer is (A + B);
begin
pragma Assert (Add (2, 2) = 5, "2 + 2 must be 5");
Put_Line ("this line is never reached");
end Assert_Fail;
The message after the colon is exactly the string given to the pragma, and
ADA.ASSERTIONS.ASSERTION_ERROR is the fully qualified name of the
exception. The line after the pragma is never printed. This immediate stop is
useful for a broken invariant, but it is too abrupt for a suite that should
report every failing case, which is why the next sections build a harness.
$ gnatmake -gnat2022 -gnatwa -gnata assert_fail.adb $ ./assert_fail raised ADA.ASSERTIONS.ASSERTION_ERROR : 2 + 2 must be 5 $ echo $? 1
The unit under test
The rest of the article tests a small package named Numbers. It
offers the greatest common divisor of two integers, a clamp that limits a
value to an interval, and a predicate that tells whether a number is even.
The package is ordinary production code; nothing in it knows about tests.
package Numbers is function Gcd (A, B : Integer) return Natural; function Clamp (Value, Low, High : Integer) return Integer; function Is_Even (Value : Integer) return Boolean; end Numbers;
The body uses Euclid's algorithm for the greatest common divisor. It takes
the absolute value of both arguments first, so the result is never negative
and Gcd (-12, 8) is four. Clamp compares the value
with the two bounds in turn, and Is_Even tests the remainder
against zero.
package body Numbers is
function Gcd (A, B : Integer) return Natural is
X : Integer := abs A;
Y : Integer := abs B;
T : Integer;
begin
while Y /= 0 loop
T := X mod Y;
X := Y;
Y := T;
end loop;
return X;
end Gcd;
function Clamp (Value, Low, High : Integer) return Integer is
begin
if Value < Low then
return Low;
elsif Value > High then
return High;
else
return Value;
end if;
end Clamp;
function Is_Even (Value : Integer) return Boolean is
begin
return Value mod 2 = 0;
end Is_Even;
end Numbers;
The implementation is small enough to read, but its behavior at the edges is what a test should pin down: negative arguments, a value exactly on a bound, and a zero remainder. Those cases are easy to get wrong and easy to check.
A harness with counters
A harness turns each check into a line of output and keeps two counters. The
package below declares Check twice: once for integers and once
for Booleans. The two profiles differ, so overloading selects the right one
from the argument types. An internal procedure Report does the
counting and printing, and Summary prints the totals.
package Test_Support is procedure Check (Name : String; Actual, Expected : Integer); procedure Check (Name : String; Actual, Expected : Boolean); function Failures return Natural; procedure Summary; end Test_Support;
The counters live in the package body, so every call to Check
updates the same totals. A failing integer check prints the expected and the
actual value on a second line, while the Boolean version prints the two names.
Failures exposes the failed count so that the main program can
choose its exit status.
with Ada.Text_IO; use Ada.Text_IO;
package body Test_Support is
Passed : Natural := 0;
Failed : Natural := 0;
procedure Report (Name : String; Ok : Boolean) is
begin
if Ok then
Passed := Passed + 1;
Put_Line ("PASS " & Name);
else
Failed := Failed + 1;
Put_Line ("FAIL " & Name);
end if;
end Report;
procedure Check (Name : String; Actual, Expected : Integer) is
begin
Report (Name, Actual = Expected);
if Actual /= Expected then
Put_Line (" expected" & Integer'Image (Expected) &
", got" & Integer'Image (Actual));
end if;
end Check;
procedure Check (Name : String; Actual, Expected : Boolean) is
begin
Report (Name, Actual = Expected);
if Actual /= Expected then
Put_Line (" expected " & Boolean'Image (Expected) &
", got " & Boolean'Image (Actual));
end if;
end Check;
function Failures return Natural is
begin
return Failed;
end Failures;
procedure Summary is
begin
Put_Line ("-----------------------------");
Put_Line (Natural'Image (Passed) & " passed," &
Natural'Image (Failed) & " failed");
end Summary;
end Test_Support;
The harness keeps the whole run going after a failure, which is the main
difference from a bare assertion. Two spaces after PASS and
FAIL line up the names, and the failing line is indented so that
it is easy to spot. The counters start at zero when the program starts, and
they are not protected, because a test suite normally runs in one task.
Running the suite
The test program calls Check once per case. The names describe
the input, so a failure is readable without opening the source. After the
last check, Summary prints the totals, and the program sets a
failure exit status only when the counter is greater than zero.
with Ada.Command_Line;
with Numbers;
with Test_Support; use Test_Support;
procedure Test_Numbers is
begin
Check ("gcd (12, 18)", Numbers.Gcd (12, 18), 6);
Check ("gcd (17, 5)", Numbers.Gcd (17, 5), 1);
Check ("gcd (-12, 8)", Numbers.Gcd (-12, 8), 4);
Check ("clamp below range", Numbers.Clamp (-3, 0, 10), 0);
Check ("clamp inside range", Numbers.Clamp (4, 0, 10), 4);
Check ("clamp above range", Numbers.Clamp (42, 0, 10), 10);
Check ("4 is even", Numbers.Is_Even (4), True);
Check ("7 is even", Numbers.Is_Even (7), False);
Summary;
if Failures > 0 then
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end if;
end Test_Numbers;
All eight checks pass, so the summary reports eight passed and zero failed,
and the process exits with status 0. The call Check ("4 is even",
..., True) selects the Boolean overload, while the calls with integer
results select the integer one. The name of the last case states the claim
being tested, not the expected value: the claim "7 is even" is false, and the
check passes because it expects false.
$ gnatmake -gnat2022 -gnatwa test_numbers.adb $ ./test_numbers PASS gcd (12, 18) PASS gcd (17, 5) PASS gcd (-12, 8) PASS clamp below range PASS clamp inside range PASS clamp above range PASS 4 is even PASS 7 is even ----------------------------- 8 passed, 0 failed $ echo $? 0
Table-driven tests
When many cases share the same shape, a table is clearer than a long list of
calls. A record holds the two arguments and the expected result, and a
constant array holds one record per case. The loop builds a name from the
arguments, calls the function, and passes the result to Check.
Adding a case means adding one line to the table.
with Ada.Command_Line;
with Numbers;
with Test_Support; use Test_Support;
procedure Table_Tests is
type Case_Rec is record
A, B : Integer;
Expected : Natural;
end record;
Cases : constant array (1 .. 5) of Case_Rec :=
[(12, 18, 6),
(17, 5, 1),
(0, 9, 9),
(-12, 8, 4),
(100, 75, 25)];
begin
for C of Cases loop
Check ("gcd (" & Integer'Image (C.A) & "," &
Integer'Image (C.B) & ")",
Numbers.Gcd (C.A, C.B), C.Expected);
end loop;
Summary;
if Failures > 0 then
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end if;
end Table_Tests;
The aggregate uses the square brackets of Ada 2022, and every record is
written positionally because the components are in the same order as the type
declaration. Integer'Image leaves a space before a non-negative
number, so the names read gcd ( 12, 18). The zero case checks
that the loop terminates when one argument is already zero, and the negative
case checks the absolute values at the start of the function.
$ gnatmake -gnat2022 -gnatwa table_tests.adb $ ./table_tests PASS gcd ( 12, 18) PASS gcd ( 17, 5) PASS gcd ( 0, 9) PASS gcd (-12, 8) PASS gcd ( 100, 75) ----------------------------- 5 passed, 0 failed $ echo $? 0
A failing case and the exit status
A test is only useful if it can fail. The next program checks three values,
but the expected result of the first one is five while
Gcd (12, 18) is six. The harness reports that mismatch and still
runs the two checks that follow, so one run shows both the failure and the
checks that passed.
with Ada.Command_Line;
with Numbers;
with Test_Support; use Test_Support;
procedure Test_Broken is
begin
Check ("gcd (12, 18)", Numbers.Gcd (12, 18), 5);
Check ("gcd (17, 5)", Numbers.Gcd (17, 5), 1);
Check ("gcd (100, 75)", Numbers.Gcd (100, 75), 25);
Summary;
if Failures > 0 then
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end if;
end Test_Broken;
The first line is the failure, with the expected and the actual value beneath
it, and the next two lines are the checks that passed. The summary counts two
passed and one failed. Because Failures is greater than zero, the
program calls Set_Exit_Status (Failure), and the shell sees
status 1. A build that runs the test executable can stop on that status, which
is how a failing suite prevents a release.
$ gnatmake -gnat2022 -gnatwa test_broken.adb
$ ./test_broken
FAIL gcd (12, 18)
expected 5, got 6
PASS gcd (17, 5)
PASS gcd (100, 75)
-----------------------------
2 passed, 1 failed
$ echo $?
1
Tests in a project file
A test suite is a separate program, so it gets its own project file. The
project below keeps production code in src and test code in
tests, lists both directories, and names the test main. The four
package files move to src, the test program moves to
tests/test_numbers.adb, and the project file sits at the top of
the tree.
project Tests is
for Source_Dirs use ("src", "tests");
for Object_Dir use "obj";
for Exec_Dir use "bin";
for Main use ("test_numbers.adb");
for Create_Missing_Dirs use "True";
package Compiler is
for Default_Switches ("Ada") use ("-gnat2022", "-gnatwa");
end Compiler;
end Tests;
Building the project compiles the three units once and links the test program
into bin. Running it produces the same eight passes and the zero
exit status. The production project would build only src and
would not see the test program at all, so test code never becomes part of the
shipped executable.
$ gprbuild -P tests.gpr Setup [mkdir] object directory for project Tests [mkdir] exec directory for project Tests Compile [Ada] test_numbers.adb [Ada] numbers.adb [Ada] test_support.adb Bind [gprbind] test_numbers.bexch [Ada] test_numbers.ali Link [link] test_numbers.adb $ ./bin/test_numbers PASS gcd (12, 18) PASS gcd (17, 5) PASS gcd (-12, 8) PASS clamp below range PASS clamp inside range PASS clamp above range PASS 4 is even PASS 7 is even ----------------------------- 8 passed, 0 failed $ echo $? 0
Next steps
This is the last article in the series. For larger projects, AUnit provides test cases, suites, fixtures, and a runner that report results in the same way as the small harness above. Its test cases are tagged types, so the tagged types and inheritance articles explain the machinery it builds on. The contracts article covers preconditions, postconditions, and invariants, which catch errors closer to their source than a test can.
Source
Ada Reference Manual: Pragma Assert
In this article we have checked claims with pragma Assert, seen
a failed assertion stop the program, built a harness that counts passes and
failures, tested a package with explicit calls and with a table of cases,
turned a failure into exit status 1, and run the suite from a GPR project. The
project files article explains the build
description that makes the suite repeatable.
Author
List all Ada tutorials.