1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
==================
Ahven User's Guide
==================
Tero Koskinen
Overview
########
Introduction
============
Ahven is a unit test library. It is modeled after
JUnit framework for Java, but some ideas are also
taken from another Ada unit test library, AUnit.
The purpose of Ahven is to be a small and portable
unit test library, which works with multiple
different Ada 95 compilers. Ahven has no
external dependencies and therefore it is easy
to build on various platforms.
Ahven tries to be compatible with utilities related
to unit testing. For example, it uses same
XML format for test results as Java tools.
This allows easy integration to CruiseControl, Ant,
and other similar programs.
License
=======
Ahven is distributed under permissive ISC license (shown below).
::
--
-- Copyright (c) 2008, 2009, 2010 Tero Koskinen <tero.koskinen@iki.fi>
--
-- Permission to use, copy, modify, and distribute this software for any
-- purpose with or without fee is hereby granted, provided that the above
-- copyright notice and this permission notice appear in all copies.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
--
You are allowed to embed Ahven into a proprietary commercial application.
Only requirement is to keep the copyright notice and the permission notice
in the source code files. You do not need to distribute Ahven's source code
if you distribute Ahven or some parts of Ahven in the binary form.
Building and Installing Ahven
#############################
To build and install Ahven source code, you need an Ada 95 compiler.
At the moment, Ahven is tested with four different
compiler families: GNAT, ObjectAda, Irvine ICCAda, and Janus/Ada.
GNAT GPL series and FSF GCC
===========================
When you have GNAT GPL or FSF GCC, the easieast way to
compile and install Ahven is to use
the *make* utility and Makefile.
When compiling using the *make* utility,
you need to tell your platform type. Currently,
only supported platform types are *unix*
and *windows*. The type can be told by
setting OS_VERSION variable to the selected platform.
::
$ make OS_VERSION=unix
If you are unable to use *make*, you
can use the GNAT project files directly.
Ahven distribution comes with three GNAT project files:
ahven.gpr, ahven_lib.gpr, and ahven_tests.gpr.
The *ahven.gpr* file is meant to be used
when compiling unit tests. The library itself is build
using the *ahven_lib.gpr* file.
To build the testsuite of the Ahven, one needs to
use the *ahven_tests.gpr* file.
Like with Makefile, you need to tell your platform type.
This time the selection happens by using an environment variable
called *OS*. The variable accepts same
values as Makefile.
::
$ OS=windows gnatmake -P ahven_lib
$ OS=windows gnatmake -P ahven_tests
Installing Library
------------------
You can install the library by using command *make install*.
By default the installation happens to the */usr/local* directory.
Alternative directory can be set by overwriting the *PREFIX* variable.
::
$ make OS_VERSION=unix PREFIX=/opt/ada install
GNAT 3.15p
==========
Version 3.15p of GNAT does not understand some features used in
the default GNAT project files. Therefore, you need to use
project files from the *contrib/gnat315p* directory.
ObjectAda
=========
There is no project file included for ObjectAda.
To compile Ahven, you need to create
a new project and import the source code of Ahven
to the project.
Irvine ICCAda
=============
Easiest way to build Ahven with ICCAda is to use *icm* utility::
C:\ahven-2.1>cd src
C:\ahven-2.1\src>icm new
C:\ahven-2.1\src>icm scan *.ad? windows\*.ad?
C:\ahven-2.1\src>icm make libmain
C:\ahven-2.1\src>cd ..\test
C:\ahven-2.1\test>icm new -search=..\src
C:\ahven-2.1\test>icm scan *.ad?
C:\ahven-2.1\test>icm make tester
Janus/Ada
=========
Directory *janusada* contains project file creation scripts for Janus/Ada.
By default, the scripts assume Janus/Ada to be installed to directory
*C:\\Janus312\\*. If that is not the case, change the path from
file *prepare.bat*.
::
C:\ahven-2.1>janusada\prepare.bat
Before compiling the library, you need to run
the preparation script *janusada\\prepare.bat*.
Then, scan the sources and create compilation script
by running *janusada\\update.bat*.
::
C:\ahven-2.1>janusada\update.bat
Now you are ready to compile the project.
This happens by running
*compile.bat* script.
::
C:\ahven-2.1>janusada\compile.bat
After a while, you should have compiled library files
in the *lib_obj* directory and
an executable called *tap_test.exe*
in the *test_obj* directory.
The executable is Ahven's test suite and if it reports
no errors, everything is working as expected.
At the time of writing (Ahven 2.1), every test
should pass with the latest version of Janus/Ada.
However, with earlier versions of Janus/Ada some tests will fail.
The failing tests are worked around in Ahven's source code, but
the test exists so that one can verify when the Janus/Ada bug
causing the failure is fixed.
Using Ahven
###########
The heart of Ahven is an abstract type called ``Test``.
It presents an entity which can be run by *a test runner*.
Types ``Test_Case`` and ``Test_Suite`` are derived from the
``Test`` type. The ``Test_Case`` type is the base type
for unit tests and the ``Test_Suite`` type is a container,
which can hold other ``Test`` objects.
Writing a Test Case
===================
To create a new test case you need to create a new package
and a new type, which is derived from
``Ahven.Framework.Test_Case``.
There are no required functions or procedures to
be implemented, but to make the test case do something
you need to override the ``Initialize`` procedure
and create at least one procedure which tests something::
-- my_tests.ads
with Ahven.Framework;
package My_Tests is
type Test is new Ahven.Framework.Test_Case with null record;
procedure Initialize (T : in out Test);
private
procedure Test_Addition;
end My_Tests;
To add tests to the test case you need to
call procedure ``Ahven.Framework.Add_Test_Routine``
during the test case initialization (in other words, in the
``Initialize`` procedure).
:ref:`testcase_a_body` shows how the
``Test_Addition`` is added to the test case.
It also shows how to set a name for the test case with
the ``Set_Name`` procedure.
.. _testcase_a_body:
A test case package body
------------------------
::
-- my_tests.adb
package body My_Tests is
procedure Initialize (T : in out Test) is
begin
Set_Name (T, "My tests");
Ahven.Framework.Add_Test_Routine
(T, Test_Addition'Access, "Addition");
end Initialize;
procedure Test_Addition is
begin
null;
end Test_Addition;
end My_Tests;
Calling Assertion Procedures
============================
To test whether a condition is true or false,
Ahven offers you three procedures. The first
procedure is :ref:`Ahven.Assert <ahven-assert>`.
It takes a boolean value and a message string as its parameters.
If the boolean value is false the ``Assert``
raises an ``Assertion_Error`` exception
with the given string. The exception is catched by the framework.
and when the test results are shown the error is also shown
with the given message.
Another assertion procedure is a generic
:ref:`Ahven.Assert_Equal <ahven-assert_equal>` procedure.
It is meant for comparing two objects of same type.
If the objects are not equal
the ``Assertion_Error`` exception
with the given message string is raised.
The third assertion procedure is simple
:ref:`Ahven.Fail <ahven-fail>` which always raises
the ``Assertion_Error`` exception.
It is handy for situations where the execution should not
reach a certain place (see :ref:`fail_example`).
.. _fail_example:
Fail in action
--------------
::
package body My_Tests is
...
procedure Test_My_Proc is
begin
begin
My_Proc (-1); -- should raise Custom_Error
Fail ("Custom_Error expected");
exception
when Custom_Error =>
null; -- expected
-- Note: the exception block should not
-- catch Assertion_Error. Otherwise
-- the assertion failure will not be noticed.
end;
end Test_My_Proc;
end My_Tests;
Composing Test Hierarchies With Test Suites
===========================================
The ``Test_Suite`` type is used to group related tests together.
You can also add other test suites to the suite and create
a hierarchy of tests.
The tests are added to the test suite using either procedure
``Add_Static_Test`` or ``Add_Test``.
The former procedure is meant for statically created tests and
it places a copy of the given test to the test suite.
The ``Add_Test`` procedure is used with dynamically created tests
and test objects of type Test_Class_Access.
At the moment, the dynamically added tests are executed first in
the order they have been added (first in, first out - FIFO)
and after them the statically added tests, also in FIFO order.
:ref:`suite_example` shows how to put test cases in a test suite.
.. _suite_example:
Suite Example
-------------
::
package body My_Tests is
...
function Get_Test_Suite return Ahven.Framework.Test_Suite is
S : Framework.Test_Suite := Framework.Create_Suite ("All");
Hello_World_Test : Hello_World.Test;
Listener_Test : Basic_Listener_Tests.Test;
begin
Framework.Add_Static_Test (S, Hello_World_Test);
Framework.Add_Static_Test (S, Listener_Test);
return S;
end Get_Test_Suite;
end My_Tests;
Running Tests
=============
The tests are run by test runners. These runners are procedures which take
either test cases or test suites as their parameters.
Currently, there exists three test runners. Ahven.Runner is the basic
runner, which prints the test results as a hierarchy. Ahven.XML_Runner
on the other hand writes the test results to an XML file, which is
understood by continuous integration systems like CruiseControl and Hudson.
The third runner is Ahven.Tap_Runner. It produces the results in
Test-Anything-Protocol (TAP) format.
The recommended way to use these test runners is to call them from
the main program:
::
with Ahven.Text_Runner;
with Ahven.Framework;
with Simple_Tests;
procedure Tester is
S : Ahven.Framework.Test_Suite := Ahven.Framework.Create_Suite ("All");
begin
Ahven.Framework.Add_Test (S, new Simple_Tests.Test);
Ahven.Text_Runner.Run (S);
end Tester;
Parameters
----------
Ahven.Text_Runner recognizes following parameters:
.. program:: tester
.. cmdoption:: -d
directory for test results
.. cmdoption:: -x
output in XML format
.. cmdoption:: -c
capture and report test outputs
.. cmdoption:: -t
specify timeout value for tests
.. cmdoption:: -q
quiet results
.. cmdoption:: -v
verbose results (default)
|