unit-testingadaaunit

Ada AUnit procedures Set_Up_Case and Tear_Down_Case called for each test


I am making unit tests in an Ada application in an existing suite based on aunit. According to their documentation and code, the procedures: Set_Up_Case and Tear_Down_Case should only run before the set of test routines. However, when I run the unit-tests (specific class or SmokeTests), then I see, they are run before and after each test. Stripped output:

2021-07-05 15:05:55 ----- Set_Up_Case -----                                                                             
2021-07-05 15:05:55 ----- Set_Up -----                                                                                                                                                                
2021-07-05 15:05:58 ----- Starting Test AAAA -----                                                              
2021-07-05 15:06:07 ----- Tear_Down -----                                                                               
2021-07-05 15:06:07 ----- Tear_Down_Case -----                                                               
           
2021-07-05 15:06:07 ----- Set_Up_Case -----                                                                             
2021-07-05 15:06:07 ----- Set_Up -----                                                                                                                                                                 
2021-07-05 15:06:10 ----- Starting Test BBBB  -----                                                                  
2021-07-05 15:06:34 ----- Tear_Down -----                                                                               
2021-07-05 15:06:34 ----- Tear_Down_Case -----  

So how can I get the behaviour as documented?


Solution

  • The Set_Up_Case and Tear_Down_Case routines are run at the start and end of a test case. The Set_Up and Tear_Down routines are run before and after a specific test routine within a test case. This is best illustrated using the minimal example shown below. The example borrows quite some example code from the AUnit cookbook.

    tests.ads (a new test case that contains 3 test routines: Test_A, Test_B and Test_C)

    with AUnit;            use AUnit;
    with AUnit.Test_Cases; use AUnit.Test_Cases;
    
    package Tests is
    
       type Test_Case is new Test_Cases.Test_Case with null record;
    
       procedure Register_Tests (T: in out Test_Case);
       function Name (T : Test_Case) return Test_String;
       
       --  Setup and tear down.
       procedure Set_Up (T : in out Test_Case);   
       procedure Set_Up_Case (T : in out Test_Case);
       
       procedure Tear_Down (T : in out Test_Case);   
       procedure Tear_Down_Case (T : in out Test_Case);   
    
       --  Test routines.
       procedure Test_A (T : in out Test_Cases.Test_Case'Class);
       procedure Test_B (T : in out Test_Cases.Test_Case'Class);
       procedure Test_C (T : in out Test_Cases.Test_Case'Class);
       
    end Tests;
    

    tests.adb (the implementation of the test case and its test routines)

    with Ada.Text_IO; use Ada.Text_IO;
    
    package body Tests is
    
       ----------------------------------------------
       -- Test Case Name and Routine Registrations --
       ----------------------------------------------
       
       procedure Register_Tests (T: in out Test_Case) is
          use Test_Cases.Registration;
       begin
          Register_Routine (T, Test_A'Access, "Test A");
          Register_Routine (T, Test_B'Access, "Test B");
          Register_Routine (T, Test_C'Access, "Test C");      
       end Register_Tests;
    
       function Name (T: Test_Case) return Test_String is
       begin
          return Format ("Tests");
       end Name;
       
       -------------------------------
       --  Test Setup and Tear Down --
       -------------------------------
    
       procedure Set_Up (T : in out Test_Case) is
       begin
          Put_Line ("| Set_Up");
       end Set_Up;
       
       procedure Set_Up_Case (T : in out Test_Case) is
       begin
          Put_Line ("Set_Up_Case");
       end Set_Up_Case;
       
       procedure Tear_Down (T : in out Test_Case) is
       begin
          Put_Line ("| Tear_Down");
       end Tear_Down;
       
       procedure Tear_Down_Case (T : in out Test_Case) is
       begin
          Put_Line ("Tear_Down_Case");
       end Tear_Down_Case;
       
       -------------------------
       -- Test Routines A/B/C --
       -------------------------
       
       procedure Test_A (T : in out Test_Cases.Test_Case'Class) is
       begin
          Put_Line ("| | Test_A");
       end Test_A;
       
       procedure Test_B (T : in out Test_Cases.Test_Case'Class) is
       begin
          Put_Line ("| | Test_B");
       end Test_B;
       
       procedure Test_C (T : in out Test_Cases.Test_Case'Class) is
       begin
          Put_Line ("| | Test_C");
       end Test_C;
    
    end Tests;
    

    a_suite.ads (a new test suite)

    with AUnit.Test_Suites;
    
    package A_Suite is
       function Suite return AUnit.Test_Suites.Access_Test_Suite;
    end A_Suite;
    

    a_suite.adb (the test suite contains 1 test case: Tests)

    with Tests;
         
    package body A_Suite is
       use AUnit.Test_Suites;
    
       --  Statically allocate test suite:
       Result : aliased Test_Suite;
    
       --  Statically allocate test cases:
       Test_1 : aliased Tests.Test_Case;
    
       function Suite return Access_Test_Suite is
       begin
          Add_Test (Result'Access, Test_1'Access);
          return Result'Access;
       end Suite;
       
    end A_Suite;
    

    main.adb (the test driver)

    with A_Suite;
    with AUnit.Run;
    with AUnit.Reporter.Text;
    
    procedure Main is
       procedure Run is new AUnit.Run.Test_Runner (A_Suite.Suite);
       Reporter : AUnit.Reporter.Text.Text_Reporter;
    begin
       Run (Reporter);
    end Main;
    

    output

    Set_Up_Case
    | Set_Up
    | | Test_A
    | Tear_Down
    | Set_Up
    | | Test_B
    | Tear_Down
    | Set_Up
    | | Test_C
    | Tear_Down
    Tear_Down_Case
    
    OK Tests : Test A
    OK Tests : Test B
    OK Tests : Test C
    
    Total Tests Run:   3
    Successful Tests:  3
    Failed Assertions: 0
    Unexpected Errors: 0