Next: , Previous: Project Attributes for gnattest, Up: Creating Unit Tests Using gnattest


26.4 Simple Example

Let's take a very simple example using the first gnattest example located in:

     <install_prefix>/share/examples/gnattest/simple

This project contains a simple package containing one subprogram. By running gnattest:

     $ gnattest --harness-dir=driver -Psimple.gpr

a test driver is created in directory "driver". It can be compiled and run:

     $ cd driver
     $ gprbuild -Ptest_driver
     $ test_runner

One failed test with diagnosis "test not implemented" is reported. Since no special output option was specified, the test package Simple.Tests is located in:

     <install_prefix>/share/examples/gnattest/simple/obj/gnattest/tests

For each package containing visible subprograms, a child test package is generated. It contains one test routine per tested subprogram. Each declaration of a test subprogram has a comment specifying which tested subprogram it corresponds to. Bodies of test routines are placed in test package bodies and are surrounded by special comment sections. Those comment sections should not be removed or modified in order for gnattest to be able to regenerate test packages and keep already written tests in place. The test routine Test_Inc_5eaee3 located at simple-test_data-tests.adb contains a single statement: a call to procedure Assert. It has two arguments: the Boolean expression we want to check and the diagnosis message to display if the condition is false.

That is where actual testing code should be written after a proper setup. An actual check can be performed by replacing the Assert call with:

     Assert (Inc (1) = 2, "wrong incrementation");

After recompiling and running the test driver, one successfully passed test is reported.