Skip to main content

AUnit tests with Parameters

·
Package Available Assertions
AdaCL.Test_Cases.Parameters Calling AUnit test with parameters

It’s tedious to create a large amount of very similar AUnit tests who only differ in the input and expected data. This package tries to remedies this by aiding you in registering the test procedure multiple and then providing test data for each call.

Specifying a test case. #

It’s suggested to create a package of each set of parameter to and each packages is contains the parts of a normal test case:

with Ada.Strings.Bounded;
with AdaCL.Test_Cases.Parameters;
with AUnit.Test_Cases;
with AUnit;

package AdaCL.Strings.Hex.Test is
   package Unsigned_8 is
	  type Test_Case is new AUnit.Test_Cases.Test_Case with private;

	  overriding procedure Register_Tests (T : in out Test_Case);
	  overriding procedure Set_Up (T : in out Test_Case);
	  overriding function Name (T : Test_Case) return AUnit.Message_String is
		 (AUnit.Format ("AdaCL.Strings.Hex.Unsigned_8"));

The Test_Case need an additional field to hold the input and expected values.

   private
	  package Bounded is new Ada.Strings.Bounded.Generic_Bounded_Length (2);
	  package Parameters is new AdaCL.Test_Cases.Parameters (
		 Input_Type    => Interfaces.Unsigned_8,
		 Expected_Type => Bounded.Bounded_String);

	  type Test_Case is new AUnit.Test_Cases.Test_Case with record
		 Parameter : Parameters.Parameter;
	  end record;
   end Unsigned_8;
end AdaCL.Strings.Hex.Test;

Implementing a test case. #

Like described in AUnit Cookbook — 4. Fixture the Fixture elements are stored as global variables inside the package body. This of course means that tests can’t run in parallel.

   package body Unsigned_8 is
	  Input    : Interfaces.Unsigned_8;
	  Expected : Bounded.Bounded_Wide_Wide_String;

The values are then set by the Set_Up procedure. This is also in accordance to the AUnit Cookbook.

	  overriding procedure Set_Up (T : in out Test_Case) is
	  begin
		 Input	  := T.Parameter.Input (T.Routine_Name);
		 Expected := T.Parameter.Expected (T.Routine_Name);

		 return;
	  end Set_Up;

The actual test procedure then used the global variables to perform the test. The test makes use of the enhanced AdaCL assertions which will produce more legible error message.

	  procedure Test_Image_8 (T : in out AUnit.Test_Cases.Test_Case'Class) is
		 Actual : constant Wide_Wide_String := Image (Input);
	  begin
		 Assert_Wide_Wide_String.Equal (Actual, Bounded.To_Wide_Wide_String (Expected), "Image");

		 return;
	  end Test_Image_8;

In this particulate case input and output date can switch places and a second test procedure can test perform a reverse test. The variables are renamed to document that input and expected have been switched.

	  procedure Test_Value_8 (T : in out AUnit.Test_Cases.Test_Case'Class) is
		 Input	  : Bounded.Bounded_Wide_Wide_String renames Unsigned_8.Expected;
		 Expected : Interfaces.Unsigned_8 renames Unsigned_8.Input;

		 Actual : constant Interfaces.Unsigned_8 := Value (Bounded.To_Wide_Wide_String (Input));
	  begin
		 Assert_Unsigned_8.Equal (Actual, Expected, "Value");

		 return;
	  end Test_Value_8;

When registering each test procedure is registered multiple time with the helper Register_Routine helper function. Note that the name (3rd) parameter needs to be unique and a single word without spaces. the

	  overriding procedure Register_Tests (T : in out Test_Case) is
		 function TBS
			(Source : in Wide_Wide_String;
			 Drop	: in Ada.Strings.Truncation := Ada.Strings.Error)
			 return Bounded.Bounded_Wide_Wide_String renames
			Bounded.To_Bounded_Wide_Wide_String;
	  begin
		 T.Parameter.Register_Routine (T, Test_Image_8'Access, "Image_00", "2 Digit", 16#00#, TBS ("00"));
		 T.Parameter.Register_Routine (T, Test_Image_8'Access, "Image_FF", "2 Digit", 16#FF#, TBS ("FF"));
		 T.Parameter.Register_Routine (T, Test_Value_8'Access, "Value_0",  "1 Digit", 16#0#,  TBS ("0"));
		 T.Parameter.Register_Routine (T, Test_Value_8'Access, "Value_00", "2 Digit", 16#00#, TBS ("00"));
		 T.Parameter.Register_Routine (T, Test_Value_8'Access, "Value_FF", "2 Digit", 16#FF#, TBS ("FF"));

		 return;
	  end Register_Tests;

From here you can register the test in a test suite and then execute it normally.

OK AdaCL.Strings.Hex.Unsigned_8 : Image_00 : 2 Digit
OK AdaCL.Strings.Hex.Unsigned_8 : Image_FF : 2 Digit
OK AdaCL.Strings.Hex.Unsigned_8 : Value_0 : 1 Digit
OK AdaCL.Strings.Hex.Unsigned_8 : Value_00 : 2 Digit
OK AdaCL.Strings.Hex.Unsigned_8 : Value_FF : 2 Digit